ASP.NET MVC 4 ViewModel with DropDownList data

后端 未结 4 1852
生来不讨喜
生来不讨喜 2020-12-14 23:35

I\'m using @html.EditorFor to render my model in edit mode, and a dropdownlist is not rendered.

Here\'s my ViewModel:

     public class          


        
相关标签:
4条回答
  • 2020-12-14 23:50

    Unfortunately, there is no built in support for drop down lists in the editor for templates. You can either write your own editor template, or use the html helper method @Html.DropDownListFor() in your view.

    Darin Dimitrov's answer to this question can walk you through the process of building an editor template for drop down lists, if you are so inclined.

    The quickest way to get this working would be to do this in your view:

    @Html.EditorFor(model => model.NoClaimsDegree)
    @Html.EditorFor( model => model.VehicleValue )
    @Html.EditorFor( model => model.EngineCapacity )
    @Html.DropDownListFor( model => model.VehicleMake, Model.VehicleMakeList, "Select a make" )
    
    0 讨论(0)
  • 2020-12-14 23:59

    I think the model for dropdownlist should be:

    public List<System.Web.Mvc.SelectListItem> VehicleMakeList {get; set;}
    

    And initialized like:

    VehicleMakeList = new List<System.Web.Mvc.SelectListItem>() 
    { 
       new SelectListItem { Value = "1", Text = "Renault" }, 
       new SelectListItem { Value = "2", Text = "Peugeot" } 
    };
    

    Or Using a Datasource:

        VehicleMakeList = db.VehicleMakers /*EF, LINQ2SQL, ADO.NET or any supported external source*/
           .Select(v=> new SelectListItem { Text = v.Name, Value = v.Id})
           .ToList();
    

    View:

    @Html.DropDownListFor(model => model.VehicleMake, Model.VehicleMakeList)
    
    0 讨论(0)
  • 2020-12-15 00:03

    binding dropdown list is easy. code as follows

    public ActionResult BindWithModel()
    {
        List<SelectListItem> items = new List<SelectListItem>();
    
        items.Add(new SelectListItem 
                    { Text = "Select Category", Value = "0", 
                            Selected = true });
        items.Add(new SelectListItem
                    { Text = "Beverages", Value = "1" });
        items.Add(new SelectListItem
                    { Text = "Condiments", Value = "2" });
        items.Add(new SelectListItem
                    { Text = "Confections", Value = "3" });
        items.Add(new SelectListItem
                    { Text = "Dairy Products", Value = "4" });
        items.Add(new SelectListItem
                    { Text = "Grains/Cereals", Value = "5" });
        items.Add(new SelectListItem
                    { Text = "Meat/Poultry", Value = "6" });
        items.Add(new SelectListItem
                    { Text = "Produce", Value = "7" });
        items.Add(new SelectListItem
                    { Text = "Seafood", Value = "8" });
    
        var model = new CategoryModel()
        {
            lstCategory = items,
            selected = 1
        };
    
        return View(model);
    }
    

    Code in view

    @model BindMvcDropdownList.Models.CategoryModel
    
    <h1>Bind MVC DropDownList with Model</h1>
    
    @Html.DropDownListFor(x => x.selected, Model.lstCategory)
    

    code taken from here http://dotnetmentors.com/mvc/how-to-bind-dropdownlist-in-asp-net-mvc-application.aspx

    0 讨论(0)
  • 2020-12-15 00:09

    Binding dropdownlist is very tricky in MVC you can do it with this in your controller get all your cities list put it in viewBag

    Create

          ViewBag.CityId = new SelectList(db.Cities, "ID", "Name");
    

    user.CityID if you are in Edit so that on edit it select the city

          ViewBag.CityId = new SelectList(db.Cities, "ID", "Name", user.CityID);
    

    in your View just do this trick

         @Html.DropDownList("CityId", "Select")
    

    this is the most simplest way I know....

    0 讨论(0)
提交回复
热议问题