0

I am looking to create a view with a drop down box which lists programming languages from a table in a database. This I have no issues with, but I also want 10 check boxes called Level 1 till Level 10. Now these 10 Levels already are in a seperate table in the db, each with thier own id etc.

My issue is, how do I create a page that lets you choose a language and then choose level 2 and 3 for example and then add them to a another db table with a foreign key for both language and levels table? I know at first it sounds simple but I need to create a controller that checks to see what levels are checked and add them. Complex part is each language has different range of skills, not all start at 1 and end at 10.

Here are examples of the tables:

Language ID Language Name

Level ID Level Name

Mapped ID LanguageID LevelID.

Thanks!

tereško
  • 58,060
  • 25
  • 98
  • 150
  • Nothing as I dont have any idea on how to implement the controller. So I thought I ask here for some ideas. Im new to MVC and C#, so far I have only done things like dropdownlist and autocomplete text boxes. – user1586899 Aug 14 '12 at 20:20
  • If you are new to MVC you could start here: http://asp.net/mvc Read some tutorials, watch some videos, tryout some things. Then show what you have tried here so that we could have a common base for discussion and see how we could help you to improve your code and implement certain aspects. – Darin Dimitrov Aug 14 '12 at 20:21
  • I have looked at tutorials on there but none seem to be about what I need. All the help online only refers to standard use for check boxes. – user1586899 Aug 14 '12 at 20:29
  • 1
    Well don't expect to get the exact code you need. It's up to you to write it. What you should do is learn the concepts in order to be able to come up with this code yourself. – Darin Dimitrov Aug 14 '12 at 20:36
  • Dont want exact code. Just a idea on how to get the tick boxes doing what I need to do. – user1586899 Aug 14 '12 at 20:41
  • 1
    So, what have you tried? Some code maybe that you would like to share with us? No code => no help. – Darin Dimitrov Aug 14 '12 at 20:41

1 Answers1

0

You need to create the models for each of your entities. Then, in your controller, send the models to the view like this:

'
' GET: /MyController/MyAction

Public Function MyAction() As ActionResult
    Dim db = New LanguageDbContext
    ViewBag.Language = New SelectList(db.Languages, "LanguageId", "Name")

    Dim db1 = New LevelsDbContext
    ViewBag.level = New SelectList(db1.Levels, "LevelId", "Name")
    Return View()
End Function

Then, you need to get the models from the view on the POST, like this:

'
' POST: /MyController/MyAction

<HttpPost()> _
Public Function MyAction(ByVal model As MyModel) As ActionResult
    If ModelState.IsValid Then
         ' update your database by accessing model like this
            Dim db As Languages = New LanguageDbContext
            Dim language As New Language
            language.Name = model.Name

            db.Languages.Add(language)
            db.SaveChanges()
     End if
 End Function

Definitely, read up on mvc at http://asp.net/mvc

user1477388
  • 20,790
  • 32
  • 144
  • 264
  • yes I have already added things to database using drop down list fine. that all works fine. my problem is having check boxes called Level 5 to Level 8 and then adding them. Would I need a for each loop that goes over selected ones and then manually add the text saying Level and then Id of the tick box? – user1586899 Aug 14 '12 at 20:36
  • Like I showed, I would create a model for all of your checkboxes and send them to the view. You can also create a list of checkboxes using a for each loop and send the list to your view. – user1477388 Aug 14 '12 at 20:41
  • Ah right, so when you choose the selected boxes, it uses the viewmodel to grab the ID of that selected Level? Thanks – user1586899 Aug 14 '12 at 20:45