简体   繁体   English

复杂对象的MVC模型绑定Html.dropdownlistfor不起作用

[英]MVC Model binding of complex objects Html.dropdownlistfor not work

When i use simle model and pass in view example a copy of the model-everything worked well. 当我使用simle模型并传递视图示例时,模型的所有副本都运行良好。

in controller 在控制器中

 AdvertisingStructure mc = context.AdvertisingStructures.Single(a => a.Id_show == id);
        if (mc == null)
        {
            return HttpNotFound();
        }

        return View(mc);

in view 鉴于

 @model  Sciencecom.Models.AdvertisingStructure
 @Html.DropDownListFor(a => a.Owner.Id, new SelectList(owners, "id", "Name"))

the value of the list selected 所选列表的值

But i inserted this model in complex objects and pass in view example a new copy-html.dropdownlist stopped working(the value of the list not selected ) 但是我将此模型插入了复杂的对象中,并在视图示例中传递了一个新的copy-html.dropdownlist停止工作(未选择列表的值)

new model 新模型

   public class CompositeModelForEdit
{
    public AdvertisingStructure Structure;

    public IEnumerable<Side> Sides;

    public IEnumerable<Surface> Surfaces;

} 

in controller 在控制器中

  AdvertisingStructure mc = context.AdvertisingStructures.Single(a => a.Id_show == id);
        List<Surface> surfaces=new List<Surface>();
        foreach (var sides in mc.Sides)
        {
            foreach (var surface in sides.Surfaces)
            {
                surfaces.Add(surface);
            }
        }
        return View(new CompositeModelForEdit() { Sides = mc.Sides, Structure = mc,Surfaces = surfaces});

in view 鉴于

@model  CompositeModelForEdit
     @Html.DropDownListFor(a => a.Structure.Owner.Id, new SelectList(owners, "id", "Name"))

have any ideas(field input display value)? 有什么想法(字段输入显示值)?

Your models are different, yet in the SelectList you use the same properties - Id, Name. 您的模型不同,但是在SelectList中使用相同的属性-Id,Name。 I'm guessing that your AdvertisingStructure class contains those properties, but the CompositeModelForEdit does not. 我猜想您的AdvertisingStructure类包含这些属性,但是CompositeModelForEdit不包含这些属性。

EDIT: First of all, do not use such a complicated viewmodel, use something simpler, for example: 编辑:首先,不要使用这么复杂的viewmodel,使用更简单的东西,例如:

public class OwnerViewModelForEdit
{
    public int OwnerId { get; set; }

    public string  Name { get; set; }

    //....
}

Then in your GET action, you can define your list of items that you want to appear in the dropdown: 然后,在GET操作中,可以定义要显示在下拉列表中的项目列表:

IEnumerable<SelectListItem> ownersList =
            owners.Select(o => new SelectListItem() {Value = o.Id.ToString(), Text = o.Name});
ViewBag.Owners = ownersList;

return View(new OwnerViewModelForEdit(.........))

finally, in the view define your dropdownlist: 最后,在视图中定义您的下拉列表:

@Html.DropDownListFor(o => o.OwnerId, (IEnumerable<SelectListItem>)ViewBag.Owners)

And then in your POST action bind the viewmodel data to your respective Owner model and you are done. 然后在POST操作中,将viewmodel数据绑定到各自的Owner模型,就可以完成了。

There are many answers on Stackoverflow, regarding mvc dropdowns. 关于MVC下拉列表,关于Stackoverflow有很多答案。 If you search a bit, you will find an even better suggestions. 如果您稍作搜索,将会发现更好的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM