我想将一个列表传递给具有 BeginCollectionItem() 的 PartialView。这是代码,
InquiryOrderViewModel
public class InquiryOrderViewModel
{
public InquiryOrder InquiryOrder { get; set; }
public List<InquiryOrderDetail> InquiryOrderDetails { get; set; }
public List<InquiryComponentDetail> InquiryComponentDetails { get; set; }
}
InquiryComponentDetail
模型
public class InquiryComponentDetail
{
[Key]
public int InquiryComponentDetailId { get; set; }
public int DesignCodeId { get; set; }
public int QualityReferenceId { get; set; }
public int Height { get; set; }
public int Length { get; set; }
public int GscmComp { get; set; }
public int Wastage { get; set; }
public int TotalYarn { get; set; }
public virtual DesignCodeQltyRef DesignCodeQltyRef { get; set; }
}
InquiryOrderIndex
视图和脚本一次渲染多个项目
@model eKnittingData.InquiryOrderViewModel
@using (Html.BeginForm("Save", "InquiryOrder"))
{
..........
<div id="cmpDts">
@foreach (var item in Model.InquiryComponentDetails)
{
}
</div>
..........
}
<script>
var prev;
$(document).on('focus', '.class03', function () {
prev = $(this).val();
}).on('change', '.class03', function () {
if (prev != "") {
$.ajax({
url: '@Url.Action("ComponentDts", "InquiryOrder")', // dont hard code your url's
type: "GET",
data: { DesignCdId: $(this).val() }, // pass the selected value
success: function (data) {
$('.cmpCls').last().replaceWith(data);
}
});
}
else {
$.ajax({
url: '@Url.Action("ComponentDts", "InquiryOrder")', // dont hard code your url's
type: "GET",
data: { DesignCdId: $(this).val() }, // pass the selected value
success: function (data) {
$(".class03 option[value='']").remove();
$('#cmpDts').append(data);
}
});
}
});
</script>
_DetailEditorRow
PartialView 为 ddls 提供并在主class03
视图中附加了它。(这只是为了向您展示什么是class03
)
@model eKnittingData.InquiryOrderDetail
@using eKnitting.Helpers
@using (Html.BeginCollectionItem("InquiryOrderDetails"))
{
<div class="editorRow">
@Html.DropDownListFor(a => a.ComponentId, (SelectList)ViewBag.CompList, "Select", new { Class = "class02" })
@Html.DropDownListFor(a => a.DesignCodeId, (SelectList)ViewBag.DCodeList, "Select", new { Class = "class03" })
@Html.TextBoxFor(a => a.NoOfParts, new { Class = "class01" })
<a href="#" class="deleteRow">delete</a>
</div>
}
and in main view it got appended to
<div id="editorRows">
@foreach (var item in Model.InquiryOrderDetails)
{
Html.RenderPartial("_DetailEditorRow", item);
}
</div>
_ComponentDetails
PartialView 渲染项目(一个列表已被一次传递)
@model List<eKnittingData.InquiryComponentDetail>
@using eKnitting.Helpers
<div class="cmpCls">
@foreach(var icd in Model)
{
using (Html.BeginCollectionItem("InquiryComponentDetails"))
{
<div class="innerCmpCls">
@Html.DisplayFor(a => icd.DesignCodeId)
@Html.DisplayFor(a => icd.QualityReferenceId)
@Html.TextBoxFor(a => icd.Height, new { Class="clsHeight clsSameHL"})
@Html.TextBoxFor(a => icd.Length, new { Class = "clsLength clsSameHL" })
@Html.TextBoxFor(a => icd.GscmComp, new { Class = "clsGscmComp clsSameHL" })
@Html.TextBoxFor(A => icd.Wastage, new { Class = "clsWastage" })
@Html.ActionLink("Fds", "View", new { id = icd.QualityReferenceId }, new { @class = "myLink", data_id = icd.QualityReferenceId })
@Html.TextBoxFor(a => icd.TotalYarn, new { Class = "clsTotalYarn" })
<br>
<div class="popFds"></div>
</div>
}
}
</div>
一次传递列表并返回 PartialView 的 ActionResult
public ActionResult ComponentDts(int DesignCdId)
{
var objContext = new KnittingdbContext();
var QltyRefList = objContext.DesignCodeQltyRefs.Where(a=>a.DesignCodeId==DesignCdId).ToList();
var iocdList = new List<InquiryComponentDetail>();
foreach(DesignCodeQltyRef dcqr in QltyRefList)
{
iocdList.Add(new InquiryComponentDetail {
DesignCodeId=dcqr.DesignCodeId,
QualityReferenceId=dcqr.QltyRefId
});
}
return PartialView("_ComponentDetails", iocdList);
}
ActionResult 为GET
var objContext = new KnittingdbContext();
var newIovm = new InquiryOrderViewModel();
var newIo = new InquiryOrder();
var iocdL = new List<InquiryComponentDetail>();
newIovm.InquiryOrder = newIo;
newIovm.InquiryComponentDetails = iocdL;
return View(newIovm);
ActionResult 为POST
public ActionResult Save(InquiryOrderViewModel inquiryOrderViewModel)
{
.........
}
当用户从下拉列表()中选择一个项目时class03
,与该项目相关的项目将使用 PartialView(_ComponentDetails'
)呈现到视图并附加。然后用户从另一个 ddl( class03
) 中选择另一个项目,相关项目被渲染并附加在之前附加的项目之后。用户可以这样继续。
渲染和附加项目工作正常。但是对于 PostBack,即使我正确地获得了列表中的项目数(我通过在POST
ActionResult 上放置一个断点来检查它),所有项目内容都显示空值。请以正确的方式指导我实现这一目标。所有帮助表示赞赏。谢谢!