2017-02-20 49 views
0

我有一個面板象下面這樣:
enter image description here如何從視圖傳遞兩個相同的對象到控制器

和視圖代碼是:

<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

參見[這個答案](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308)的一些選項 –

+0

@StephenMuecke感謝您的答覆。我使用了該解決方案及其工作,但它傳遞了正確的計數,但爲空contains.do你知道爲什麼嗎? – Mojtaba

+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="[email protected](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 POST方法應該接受enumarable列表:

[HttpPost] 
public ActionResult Create(IEnumerable<JobProductlist> job) 
{ 
    return View(job); 
} 
+0

爲什麼不期望返回ViewModel,而不是'IEnumerable '? – ColinM

相關問題