Search code examples
c#asp.net-mvcviewbag

send and retrive in the model (select - dropDown) MVC C#


I like traffic and retrieve a select (dropdown) the MVC Model in C #?

my html

<select id = 'namePerson' name = 'namePerson'> 
     <option value = '@ idPerson'> @ namePerson </ option> 
</ select> 

my controller

ViewBag.namePerson = mod.namePerson; 
ViewBag.idPerson = mod.idPerson;

By posting form is filled only IdPerson.

Thank you!


Solution

  • Why aren't you using?

    @Html.DropDownListFor()
    

    By other hand, instead of using ViewBag, you could use

    ViewData["namePerson"]
    ViewDate["idPerson"]
    

    And get your HTML like this

    <select id = 'namePerson' name = 'namePerson'> 
        <option value = @ViewData["idPerson"]>@ViewData["namePerson"]</ option> 
    </ select> 
    

    You only need to be sure that you're actually sending data and everything should work.