Asp.net mvc ASP.NET MVC中将复杂视图模型转换为razor表单

Asp.net mvc ASP.NET MVC中将复杂视图模型转换为razor表单,asp.net-mvc,asp.net-mvc-4,razor,Asp.net Mvc,Asp.net Mvc 4,Razor,我有一个复杂的视图模型。我怎样才能做一张表格呢 public class ProductViewModel{ public string ProductName {get;set;} public double Price {get;set;} public BrandViewModel brand {get;set;} public CategoryViewModel category {get;set;} } public class BrandViewModel{ p

我有一个复杂的视图模型。我怎样才能做一张表格呢

public class ProductViewModel{
  public string ProductName {get;set;}
  public double Price {get;set;}
  public BrandViewModel brand {get;set;}
  public CategoryViewModel category {get;set;}
}

public class BrandViewModel{
  public string BrandName {get;set;}
  public string BrandRegYear {get;set;}
}

public class CategoryViewModel{
  public string CategoryName {get;set;}
  public bool IsISOStandard {get;set;}
}


如何为ProductViewModel创建razor表单?

我假设品牌和类别是预先填充的表/模型? 首先,可能在这两个表上添加一个Id列(如果您还没有)

然后在视图模型中,只添加Id列(即删除
public BrandViewModel brand{get;set;}
并添加
public int BrandId{get;set;}

也许可以为UI使用下拉列表(或列表),如果列表较小,则将其添加到视图包中(如果列表较大,则使用client/JS/ajax调用)

比如:

<div class="form-group">
    @Html.LabelFor(model => model.BrandId)
    <div>
        @Html.DropDownListFor(model => model.BrandId, ViewBag.Brands as SelectList, "Please select", new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.BrandId, "Blah blah blah bad input blah blah", new { @class = "text-danger" })
    </div>
</div>

@LabelFor(model=>model.BrandId)
@Html.DropDownListFor(model=>model.BrandId,ViewBag.Brands作为SelectList,“请选择”,新建{@class=“form control”})
@ValidationMessageFor(model=>model.BrandId,“废话废话输入错误废话”,新的{@class=“text danger”})
@Html.EditorFor(model=>model.brand.BrandName)@Html.EditorFor(model=>model.category.CategoryName)
以及其他复合视图模型属性,如
@Html.EditorFor(model=>model.ProductName)
,如果我理解正确的话。