[英]Cannot get the Partialview Model in Parent Controller
我正在創建一個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>
我的觀點
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; }
}
任何人都可以提出我的錯誤是什么,或者作為MVC的Iam的新方法來進行此數據輸入?
您在for
循環中使用BeginCollectionItem()
會for
您的name
屬性添加前綴, 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>
}
請注意,當您要與javascript / ajax一起在視圖中從集合中動態添加和刪除項目時,將使用BeginCollectionItem()
方法BeginCollectionItem()
答案 )。 如果確實要這樣做,則需要為模型創建一個局部視圖(例如) _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)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.