Selected Dropdown using HTML Helpers in mvc 4

前端 未结 3 1617
情深已故
情深已故 2021-01-29 08:59

I need to select the dropdown in html helper. I pass my data in IEnumerable using ViewData please some one help me how to select it with value.

My Code is



        
3条回答
  •  走了就别回头了
    2021-01-29 09:43

    If the property your binding to is named BrandMaster then it should be

    @Html.DropDownListFor(m => m.BrandMaster, ViewData["Brand"] as IEnumerable, new { @class = "form-control Repeat TableMust"})
    

    If the value of BrandMaster matches the value of one of the SelectList items, then it will be selected by default.

    Always use the strongly types helpers!

    Edit

    To use create a dropdown for the property of a collection, you need to use a custom EditorTemplate for your model

    public class MyModel
    {
      public int BrandMaster { get; set; }
      public string AnotherProperty { get; set; }
    }
    

    MyModel.cshtml (in Views/Shared/EditorTemplates

    @model MyModel
    @Html.DropDownListFor(m => m.BrandMaster, ViewData["Brand"] as IEnumerable, new { @class = "form-control Repeat TableMust"})
    @Html.TextBoxFor(m => m.AnotherProperty)
    

    Main View

    @model IEnumerable
    @using (Html.BeginForm())
    {
      @Html.EditorFor(m => m, new { Brand = ViewData["Brand"])
      ....
    }
    

提交回复
热议问题