[RESOLVED]how to save dropdown list value inside database in MVC

Hi

I have written code for creation of records manually in cshtml file

<div class="editor-label">
@Html.LabelFor(model => model.PartyCode)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PartyCode, new { style = "width:250px" })
@Html.ValidationMessageFor(model => model.PartyCode)
</div>

<td>
<div class="editor-field">
@Html.DropDownList("Mobiledropdown3", new List<SelectListItem>
{
new SelectListItem { Text = "HTC DESIRE", Value = "1"},
new SelectListItem { Text = "Moto G", Value = "2"},
new SelectListItem { Text = "GO mobiles", Value = "3"}
}, "Select Mobile")
</div>
</td>

I have table Mobilestore in sql server

MobileNo int

price int.

for example if i save in Mobilestore table
as MobileNo———1
Price————->5000

while populating data from database,
‘HTC DESIRE’ should be displayed in the dropdownlist along with other values
Also while editing, inside dropdownlist ‘HTC DESIRE’ should be displayed along with other values in list.
Please help me to get solution

Thanks

chandran

Hi rchandran.nr,

Thanks for your post.

You can use ViewBag.please refer to the following link:

#Populate Selected Item in DropDownList in Edit Mode MVC

http://www.itorian.com/2013/04/populate-selected-item-in-dropdownlist.html

More information:

#Binding Dropdownlist With Database In MVC

http://www.c-sharpcorner.com/UploadFile/4d9083/binding-dropdownlist-in-mvc-in-various-ways-in-mvc-with-data/

Hope this can be helpful.

Best Regards,

Eileen

Leave a Reply