Get the selected value of a DropDownList. Asp.NET MVC

后端 未结 2 757
醉梦人生
醉梦人生 2020-12-11 03:21

I\'m trying to populate a DropDownList and to get the selected value when I submit the form:

Here is my model :

public class Book
{
    public Book         


        
2条回答
  •  眼角桃花
    2020-12-11 03:44

    You can use DropDownListFor as below, It so simpler

    @Html.DropDownListFor(m => m.Id, new SelectList(Model.Books,"Id","Name","1"))
    

    (You need a strongly typed view for this -- View bag is not suitable for large lists)

       public ActionResult Action(Book model)
       {
            if (ValidateFields()
            {
                var Id = model.Id;
            ...        
    

    I think this is simpler to use.

提交回复
热议问题