,视图代码为:
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingFour">
<h4 class="panel-title">
<a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseFour" aria-expanded="false" aria-controls="collapseFour">
Product Details
</a>
</h4>
</div>
<div id="collapseFour" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingFour">
<div class="panel-body">
<a class="btn btn-lg btn-primary btn-add-panel" > <i class="glyphicon glyphicon-plus"></i> Add new Product</a>
<div id="propanel"></div>
<br />
<div class="panel panel-default template" >
<div class="panel-heading">
<span class="glyphicon glyphicon-remove-circle pull-right "></span>
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse5">
New Product
</a>
</h4>
</div>
<div id="collapse5" class="panel-collapse collapse">
<div class="panel-body">
<div class="form-group">
@Html.LabelFor(model => model.JobProduct.Detail, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.JobProduct.Detail, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.JobProduct.Detail, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.JobProduct.UnitPrice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.JobProduct.UnitPrice, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.JobProduct.UnitPrice, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.JobProduct.Count, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.JobProduct.Count, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.JobProduct.Count, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
这个面板可能有一个或多个产品。在viewmodel
我得到一个产品类型的对象。我想在插入的产品view
中创建列表并将其作为列表传递给控制器。我想在视图中创建列表并在其中添加对象
如何插入产品并作为列表发送到controller
?
答案 0 :(得分:1)
您的基类:
public class JobProduct
{
public string Detail{get;set;}
public decimal UnitPrice{get;set;}
public int Count{get;set;}
}
模特课:
public class JobList
{
public IEnumerable<JobProduct> JobProductList { get; set; }
}
将get方法中的JobProductList
传递给视图:
@for (int i = 0; i < Model.JobProductList.length; i++)
{
<div id="collapse@(i+1)" class="panel-collapse collapse">
<div class="panel-body">
<div class="form-group">
@Html.LabelFor(model => model.JobProductList[i].Detail, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.JobProductList[i].Detail, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.JobProductList.Detail, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.JobProductList[i].UnitPrice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.JobProductList[i].UnitPrice, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.JobProductList[i].UnitPrice, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.JobProductList[i].Count, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.JobProductList[i].Count, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.JobProductList[i].Count, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
}
您的最终Create
帖子方法应该接受可阻止列表:
[HttpPost]
public ActionResult Create(IEnumerable<JobProductlist> job)
{
return View(job);
}