[英]Passing a list to partialview, BeginCollectionItem()
I want to pass a list to PartialView that has BeginCollectionItem(). 我想将一个列表传递给具有BeginCollectionItem()的PartialView。 Here is the code, 这是代码,
InquiryOrderViewModel
public class InquiryOrderViewModel
{
public InquiryOrder InquiryOrder { get; set; }
public List<InquiryOrderDetail> InquiryOrderDetails { get; set; }
public List<InquiryComponentDetail> InquiryComponentDetails { get; set; }
}
InquiryComponentDetail
model 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
View and the Script to render multiple items at once InquiryOrderIndex
View和脚本以一次渲染多个项目
@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>
The _DetailEditorRow
PartialView which gives ddls with class03
and in main view where it got appended.(This is just to show you what is class03
) _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 to render items(a list has been passed at once) _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>
ActionResult that Passes a list at once and returns the PartialView 一次传递列表并返回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 for GET
GET
ActionResult
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 for POST
POST
ActionResult
public ActionResult Save(InquiryOrderViewModel inquiryOrderViewModel)
{
.........
}
When user selects an item from a dropdownlist( class03
), the items related to that item are rendered to the view using the PartialView( _ComponentDetails'
) and get appended. 当用户从下拉列表( class03
)中选择一个项目时,与该项目相关的项目将使用PartialView( _ComponentDetails'
)呈现到视图中并附加。 Then user selects another item from another ddl( class03
), the related items are rendered and appended after earlier appended ones. 然后,用户从另一个ddl( class03
)中选择另一个项目,相关的项目将在较早添加的项目之后呈现并添加。 User can go on like this. 用户可以这样继续。
Rendering and appending items works fine. 渲染和附加项目效果很好。 But for the PostBack even though i get the number of items in the list correctly(I checked it by putting a break point on POST
ActionResult ) all items content show null values. 但是对于PostBack,即使我正确获得列表中的项目数(我通过在POST
ActionResult上放置一个断点来检查它),所有项目的内容都显示为空值。 Pls guide me in the correct way for achieving this. 请以正确的方式指导我实现这一目标。 All help appreciated. 所有帮助表示赞赏。 Thanks! 谢谢!
Your _ComponentDetails
view is generating form controls that have name attributes that look like (where ###
is a Guid
) 您的_ComponentDetails
视图正在生成具有如下名称属性的表单控件(其中###
是Guid
)
name="InquiryComponentDetail[###].icd.Height"
which does not match your model because typeof InquiryComponentDetail
does not contain a property named icd
. 这与您的模型不匹配,因为typeof InquiryComponentDetail
不包含名为icd
的属性。 In order to bind to your model, your name
attribute would need 为了绑定到您的模型,您的name
属性需要
name="InquiryComponentDetail[###].Height"
To generate the correct html, you will need 2 partials 要生成正确的html,您将需要2个局部变量
_ComponentDetailsList.cshtml
(this will be called by the ComponentDts()
method using return PartialView("_ComponentDetailsList", iocdList);
) _ComponentDetailsList.cshtml
(这将由ComponentDts()
方法使用return PartialView("_ComponentDetailsList", iocdList);
)
@model List<eKnittingData.InquiryComponentDetail>
<div class="cmpCls">
@foreach(var item in Model)
{
Html.RenderPartial("_ComponentDetails", item);
}
</div>
_ComponentDetails.cshtml
@model eKnittingData.InquiryComponentDetail
using (Html.BeginCollectionItem("InquiryComponentDetails"))
{
<div class="innerCmpCls">
@Html.DisplayFor(a => a.DesignCodeId)
@Html.DisplayFor(a => a.QualityReferenceId)
@Html.TextBoxFor(a => a.Height, new { @class="clsHeight clsSameHL"}) // use @class, not Class
@Html.TextBoxFor(a => a.Length, new { Class = "clsLength clsSameHL" })
....
</div>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.