2017-08-02 82 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循环帮助 –