2017-08-02 85 views
0

enter image description here 我正在創建一個DataEntry屏幕,其中有一個使用ajax級聯的頂部 的三個下拉列表。部分視圖也使用Ajax呈現,該視圖將顯示與選擇用戶在下拉菜單上所做的所有項目。局部視圖由一個帶有多個捲筒的表格組成。無法在父控制器中獲得部分視圖模型

我無法獲取用戶在我的控制器中的部分視圖(表格)中選擇的數據集合。

我的控制器

[HttpGet] 
    public ActionResult ApproveLaysheet() 
    { 
     LaySheetShortageViewModel mdl = new LaySheetShortageViewModel(); 
     ViewBag.AtcID = new SelectList(db.AtcMasters.Where(o => o.IsClosed == "N"), "AtcId", "AtcNum"); 
     return View(mdl); 
    } 
     [HttpGet] 
    public PartialViewResult GetRollView(decimal[] SelectedOurStyle) 
    { 
     LaySheetShortageViewModel model = new LaySheetShortageViewModel();   
     LaysheetRollRepository lyipores = new LaysheetRollRepository(); 
     model.rolldetailcollection= lyipores.getlaysheetRollData(SelectedOurStyle); 
     return PartialView("LaySheetRollView",model); 
    } 
[HttpPost] 
    public ActionResult ApproveLaysheet(LaySheetShortageViewModel Model) 
    {  // not gretting the value of rolldetailcollection here 
     return View(); 
    } 

我查看

@model ArtWebApp.Areas.ArtMVC.Models.ViewModel.LaySheetShortageViewModel 
<script type="text/javascript"> 
    $(document).ready(function() {  
     $("#Show").click(function (e, params) {   
      debugger; 
      var SelectedOurStyle = new Array(); 
      SelectedOurStyle = $("#LaySheetID").chosen().val();    
      if (SelectedOurStyle != null) 
      { 
      $.ajax({ 
        url: "@Url.Action("GetRollView", "ApproveLaysheet")", 
        traditional: true, 
        data: { 'SelectedOurStyle': SelectedOurStyle }, 
        type: "GET", 
        success: function (fooBarHTML) { 
         $("#output").html(fooBarHTML); 
        }, 
        error: function (xhr, status, errorThrown) { 
         //... 
        } 
       });   

     } 

     }); 

    }); 

</script> 
@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>SampCutReqMaster</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      <div class="row"> 
       <div class="col-md-2"> 
        @Html.Label("Atcc#", new { @class = "control-label col-md-2" }); 
       </div> 
       <div class="col-md-10"> 
        @Html.DropDownList("AtcID", null, htmlAttributes: new { @class = "chosen-select form-control" })     
       </div> 
      </div>   

     </div> 


     <div class="form-group"> 
      <div class="row"> 
       <div class="col-md-2"> 
        @Html.Label("OurStyle#", new { @class = "control-label col-md-2" }); 
       </div> 
       <div class="col-md-10"> 
        @Html.DropDownList("OurStyleID", new MultiSelectList(string.Empty, "Value", "Text") , null, htmlAttributes: new { @class = "chosen-select form-control", @multiple = "multiple" }) 

       </div> 

      </div>   

     </div> 

     <div class="form-group"> 
      <div class="row"> 
       <div class="col-md-2"> 
        @Html.Label("LaySheet#", new { @class = "control-label col-md-2" }); 
       </div> 
       <div class="col-md-10"> 
        @Html.DropDownList("LaySheetID", new MultiSelectList(string.Empty, "Value", "Text"), null, htmlAttributes: new { @class = "chosen-select form-control", @multiple = "multiple" }) 

       </div> 
      </div> 
     </div> 

     <div id='output' class=""> 
      <!-- Partialview Comes here --> 
     </div> 


</div> 
} 

我的局部視圖

@using HtmlHelpers.BeginCollectionItem 
@model ArtWebApp.Areas.ArtMVC.Models.ViewModel.LaySheetShortageViewModel 
<script src="~/JQuery/GridJQuery.js"></script> 
<script> 

</script> 
<div class="container"> 

    <table class="table table-bordered table-striped table-responsive"> 
     <tr> 
      <th> 
       @Html.CheckBox("SelectAll") 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.approvelaysheetModel.LaySheetDet_PK) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.approvelaysheetModel.LayCutNum) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.approvelaysheetModel.RollNum) 
      </th>   
    </tr> 
     @if (Model != null) 
     { 
     for (int i = 0; i < Model.rolldetailcollection.Count; i++) 
      { 
       using (Html.BeginCollectionItem("rolldata")) 
       { 
       <tr> 
        <td> 
         @Html.EditorFor(modelItem => Model.rolldetailcollection[i].IsSelected, new { @onclick = "Check_ClickNew(this)" }) 
        </td> 
        <td> 
         @Html.EditorFor(modelItem => Model.rolldetailcollection[i].LaySheetDet_PK) 
         @Html.HiddenFor(model => Model.rolldetailcollection[i].LaySheetDet_PK, new { htmlAttributes = new { @class = "form-control" } }) 
        </td> 
        <td> 
         @Html.DisplayFor(modelItem => Model.rolldetailcollection[i].LayCutNum) 
        </td> 
        <td> 
         @Html.DisplayFor(modelItem => Model.rolldetailcollection[i].RollNum) 
        </td>     
        </tr> 
       } 
      } 

     } 
     </table> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input id="Submit" type="submit" value="Submit Fabric request" class="btn btn-default show" /> 
     </div> 
    </div> 
</div> 

我viewModals

public class ApprovelaysheetModel 
    { 
     public Decimal ApprovelaysheetModelID { get; set; } 

     [Display(Name ="ID")] 
     public Decimal LaySheetDet_PK { get; set; } 
     public Boolean IsSelected { get; set; } 
     public Decimal LaySheet_PK { get; set; } 
    } 

    public class LaySheetShortageViewModel 
    { 
     [Key] 
     [Display(Name = "ID")] 
     public Decimal ShortageID { get; set; } 
     public int Location_pk { get; set; } 
     public int Atcid { get; set; } 
     public int OurstyleID { get; set; }  } 
     public List<ApprovelaysheetModel> rolldetailcollection { get; set; } 
    } 

任何人都可以提出什麼我的錯誤或者這樣做的DataEntry如蔭新MVC

+1

有多種問題與你的代碼有關,但是你的集合dos沒有綁定的原因是因爲你使用'BeginCollectionItem()'將'name'屬性用'rolldata [xxx]'作爲前綴,這與你的模型沒有任何關係。你爲什麼使用'BeginCollectionItem()'(這只是爲了動態添加和刪除你的集合中你看起來沒有做的項目)?如果你確實需要這個功能,你不要使用'for'循環 –

+0

您好步驟我曾嘗試以前沒有開始集合,只是使用foreach(Model.rolldetailcollection var項目),但它並沒有幫助我得到rolldetail集合null現在開始收集它顯示正確的計數,但裏面的項目沒有綁定。我需要在回發中獲取表格的所有行。 –

+1

你不能使用'foreach'循環(儘管你需要如果你使用'BeginCollectionItem',但這是一個不同的問題)。你需要一個'for'循環或'EditorTemplate'(參考[這個答案](http:// stackoverflow。COM /問題/ 30094047/HTML表到ADO淨數據表/ 30094943#30094943))。如果你沒有動態添加和刪除視圖中的集合中的項目,那麼只需刪除'using(Html.BeginCollectionItem(「rolldata」))'代碼行,它將正確綁定 –

回答

0

你的for循環內使用BeginCollectionItem()是你的前綴屬性name,使他們不再與您的車型性能更好的方法。相反,產生

<input type="checkbox" name="rolldetailcollection[0].IsSelected" ... /> 

你現在產生

<input type="checkbox" name="rolldata[####].rolldetailcollection[0].IsSelected" ... /> 

其中####Guid

刪除BeginCollectionItem()代碼,使你的循環是

for (int i = 0; i < Model.rolldetailcollection.Count; i++) 
{ 
    <tr> 
     <td> 
      @Html.EditorFor(m => m.rolldetailcollection[i].IsSelected, new { @onclick = "Check_ClickNew(this)" }) 
     </td> 
     <td> 
      @Html.EditorFor(m => m.rolldetailcollection[i].LaySheetDet_PK) 
      // Note there is no point adding html attributes for a hidden input 
      @Html.HiddenFor(m => m.rolldetailcollection[i].LaySheetDet_PK) 
     </td> 
     ....     
    </tr> 
} 

注意,BeginCollectionItem()方法使用時要動態添加和使用JavaScript/AJAX一起刪除視圖從集合項目(詳見例如this answer)。如果你想做到這一點,那麼你需要創建一個局部視圖(比方說)_ApprovelaysheetModel.cshtml爲模型

@model ApprovelaysheetModel 
// Note the name needs to match the collection property name 
@using (Html.BeginCollectionItem("rolldetailcollection")) 
{ 
    .... 
    @Html.EditorFor(m => m.LaySheetDet_PK) 
    .... 
} 

,然後在主視圖您使用foreach循環

@foreach(var item in Model.rolldetailcollection) 
{ 
    @Html.Partial("_ApprovelaysheetModel", item) 
} 
+0

刪除begincollection和for循環幫助 –