Click here to Skip to main content
15,938,325 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello, i dnt knw how to get the selected value and then save it in database.
I have a country entity: countryId, country_Name

and i also have a profile entity with this property:
C#
public selectlist countrylist {get; set;}


then in the create action of the controller, i used linq to get the list of countries from database

and in my view i implemented the dropdownlist like this:
HTML
@html.dropdownlist("ddlcountry", model.countrylist)
but if i submit it, i get null value error for contrylist.

Please, how can i save it in database. In profile table, i now see countrylist_datavalue & countrylist_datatext
Posted
Updated 16-May-15 1:59am
v2

1 solution

Hey, you need to add one more property to bind your result in the form.

Like, public int CountryId { get; set; }

Here is a little change in your code to bind the value with this property.
@Html.DropdownList(Model.CountryId, Model.CountryList)

Now, on submitting you will get the selected value in Model.CountryId. And you can directly save this selected value into the database.

Also, you can set by default value in the CountryId property.

Hope it works !!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900