我正在创建一个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
有多种问题与你的代码有关,但是你的集合dos没有绑定的原因是因为你使用'BeginCollectionItem()'将'name'属性用'rolldata [xxx]'作为前缀,这与你的模型没有任何关系。你为什么使用'BeginCollectionItem()'(这只是为了动态添加和删除你的集合中你看起来没有做的项目)?如果你确实需要这个功能,你不要使用'for'循环 –
您好步骤我曾尝试以前没有开始集合,只是使用foreach(Model.rolldetailcollection var项目),但它并没有帮助我得到rolldetail集合null现在开始收集它显示正确的计数,但里面的项目没有绑定。我需要在回发中获取表格的所有行。 –
你不能使用'foreach'循环(尽管你需要如果你使用'BeginCollectionItem',但这是一个不同的问题)。你需要一个'for'循环或'EditorTemplate'(参考[这个答案](http:// stackoverflow。COM /问题/ 30094047/HTML表到ADO净数据表/ 30094943#30094943))。如果你没有动态添加和删除视图中的集合中的项目,那么只需删除'using(Html.BeginCollectionItem(“rolldata”))'代码行,它将正确绑定 –