我已经在这里待过几次了,如果可能的话,我会喜欢看几双“新鲜”的眼睛。
在我的应用程序中,我有一个表单,我使用的模型包含一些原始类型和两个列表。我也在使用Kendo网格。
我的问题是,当用户提交时,模型到达正常,但两个列表中的一个返回0个元素......! (永不为空)
到达的列表是我在Kendo Grid中创建的列表。
返回空的列表是List ProductItemlist,它是在局部视图中生成的(也尝试不使用局部视图)。
事情是,如果我这样做在控制器上:
string test = Request.Form [“ProductItemlist [0] .ProductItemId”],
我得到了我想要的值,所以我认为问题必须在映射中。
然而,我没有发现它......
发生了什么......?在此先感谢您的帮助!
我的模特:
public class PurchaseRegistrationProductEditVM
{
public int ClientId { get; set; }
public int ProductId { get; set; }
public int EnterpriseId { get; set; }
public int HeadquarterId { get; set; }
public string Name { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public int StatusId { get; set; }
public int? RUC { get; set; }
public string RUCName { get; set; }
public string RUCAddress { get; set; }
public List<PurchaseRegistrationProductItemEditVM> ProductItemlist { get; set; }
public List<ClientProductPurchaseRegistryComissionEditVM> ComissionList { get; set; }
}
Model PurchaseRegistrationProductItemEditVM:
public class PurchaseRegistrationProductItemEditVM{
public int ProductItemId { get; set; }
public string ProductItemName { get; set; }
public int ProductItemTypeId { get; set; }
public string ProductItemTypeName { get; set; }
public DateTime? StartCourseDate { get; set; }
public DateTime? EndCourseDate { get; set; }
public decimal Amount { get; set; }
public int ExpiryDays { get; set; }
public string Size { get; set; }
public int DiscountTypeId { get; set; }
public string DiscountTypeName { get; set; }
public decimal? Discount { get; set; }
public int? MonthlyPaymentDueDay { get; set; }
public decimal? MonthlyPaymentDuePenalty { get; set; }
public DateTime MatriculationStartDate { get; set; }
public string MatriculationObservation { get; set; }
}
Model:ClientProductPurchaseRegistryComissionEditVM
public class ClientProductPurchaseRegistryComissionEditVM
{
public int Id { get; set; }
public int ClientProductPurchaseRegistryId { get; set; }
public string EmployeeName { get; set; }
public int EmployeeId { get; set; }
public string Observations { get; set; }
}
我的观点:
@model PurchaseRegistrationProductEditVM
@{
Layout = "~/Areas/Backoffice/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("ProcessPurchases", "Client"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.ClientId)
@Html.HiddenFor(model => model.EndDate)
@Html.HiddenFor(model => model.EnterpriseId)
@Html.HiddenFor(model => model.HeadquarterId)
@Html.HiddenFor(model => model.Name)
@Html.HiddenFor(model => model.ProductId)
@Html.HiddenFor(model => model.ProductItemlist)
@Html.HiddenFor(model => model.StartDate)
@Html.HiddenFor(model => model.StatusId)
@Html.Partial("_PartialViewProductItem")
<div class="form_sep">
@Html.Label(Resources.Client_Pending_Payment_Label_Comission)
@(Html.Kendo().ComboBox()
.Name("EmployeeComissionComboBox")
.Placeholder(Resources.Employee_ComboBox)
.DataTextField("Text")
.DataValueField("Value")
.Filter(FilterType.Contains)
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetEmployeesComboBox", "Client", new { EnterpriseId = @Model.EnterpriseId });
})
.ServerFiltering(true);
})
)
</div>
<div class="form_sep">
@Html.Label(Resources.Client_Purchase_Registry_Comission_Field_Observations)
@Html.TextBox("PurchaseComissionObservations", "", new { maxlength = 50, size = 10, @class = "form-control" })
</div>
<div class="form_sep">
@(Html.Kendo().Button()
.Name("EmployeeComissionAddButton")
.HtmlAttributes(new { type = "button", @class = "k-primary" })
.Content(Resources.Client_SelectedPayments_Button_Add_Employee_Comission)
.Events(ev => ev.Click("onClickAddEmployeeComission"))
)
</div>
<div class="form_sep">
@Html.Label(Resources.Client_Purchase_Registry_Grid_Added_Employees)
@(Html.Kendo().Grid<ClientProductPurchaseRegistryComissionEditVM>()
.Name("PurchaseCommissionGrid")
.HtmlAttributes(new { style = "height:150px;" })
.Columns(columns =>
{
columns.Bound(o => o.EmployeeName).Filterable(f => f.Cell(c => c.ShowOperators(false))).ClientTemplate("#= EmployeeName #<input type='hidden' name='ComissionList[#= indexPurchaseComissionGrid(data)#].EmployeeName' value='#= EmployeeName #' />");
columns.Bound(o => o.EmployeeId).Hidden().Filterable(f => f.Cell(c => c.ShowOperators(false))).ClientTemplate("#= EmployeeId #<input type='hidden' name='ComissionList[#= indexPurchaseComissionGrid(data)#].EmployeeId' value='#= EmployeeId #' />");
columns.Bound(o => o.Observations).Filterable(f => f.Cell(c => c.ShowOperators(false))).ClientTemplate("#= Observations #<input type='hidden' name='ComissionList[#= indexPurchaseComissionGrid(data)#].Observations' value='#= Observations #' />");
columns.Command(command =>
{
command.Custom("Remove").Text(Resources.Site_Link_Remove).Click("onDeleteEmployeeComission");
});
})
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(5)
)
.Pageable()
.Sortable()
.Scrollable())
</div>
<div class="form_sep">
@Html.LabelFor(model => model.RUC)
@Html.TextBoxFor(model => model.RUC, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.RUC)
</div>
<div class="form_sep">
@Html.LabelFor(model => model.RUCName)
@Html.TextBoxFor(model => model.RUCName, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.RUCName)
</div>
<div class="form_sep">
@Html.LabelFor(model => model.RUCAddress)
@Html.TextBoxFor(model => model.RUCAddress, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.RUCAddress)
<input id="btnSubmit" type="submit" value="@Resources.FO_Client_Link_Buy_Product" class="btn btn-default" />
@Html.ActionLink(Resources.Site_Link_Back, "PurchaseProduct/" + @Model.ClientId, "Client", new { Area = "Frontoffice" }, new { @class = "btn btn-default" })
}
部分视图:
@model PurchaseRegistrationProductEditVM
@for (int i = 0; i < Model.ProductItemlist.Count; i++)
{
@Html.HiddenFor(model => model.ProductItemlist[i].Amount)
@Html.HiddenFor(model => model.ProductItemlist[i].Discount)
@Html.HiddenFor(model => model.ProductItemlist[i].DiscountTypeId)
@Html.HiddenFor(model => model.ProductItemlist[i].DiscountTypeName)
@Html.HiddenFor(model => model.ProductItemlist[i].EndCourseDate)
@Html.HiddenFor(model => model.ProductItemlist[i].ExpiryDays)
@Html.HiddenFor(model => model.ProductItemlist[i].MonthlyPaymentDueDay)
@Html.HiddenFor(model => model.ProductItemlist[i].MonthlyPaymentDuePenalty)
@Html.HiddenFor(model => model.ProductItemlist[i].ProductItemId)
@Html.HiddenFor(model => model.ProductItemlist[i].ProductItemName)
@Html.HiddenFor(model => model.ProductItemlist[i].ProductItemTypeId)
@Html.HiddenFor(model => model.ProductItemlist[i].ProductItemTypeName)
@Html.HiddenFor(model => model.ProductItemlist[i].Size)
@Html.HiddenFor(model => model.ProductItemlist[i].StartCourseDate)
if (Model.ProductItemlist[i].ProductItemTypeId == 1)
{
DateTime date = DateTime.Now;
if (date < Model.ProductItemlist[i].StartCourseDate.Value)
{
date = Model.ProductItemlist[i].StartCourseDate.Value;
}
//Case Course
<div class="form_sep">
<b>@Resources.Site_Link_Course @Html.LabelFor(model => model.ProductItemlist[i].ProductItemName)</b>
</div>
<div class="form_sep">
@Html.LabelFor(model => model.ProductItemlist[i].MatriculationStartDate)
@(Html.Kendo().DatePickerFor(model => model.ProductItemlist[i].MatriculationStartDate)
.Animation(true)
.Culture("pt-PT")
.Format("dd-MM-yyyy")
.Value(date)
.Min(Model.ProductItemlist[i].StartCourseDate.Value)
.Max(Model.ProductItemlist[i].EndCourseDate.Value)
)
@Html.ValidationMessageFor(model => model.ProductItemlist[i].MatriculationStartDate)
</div>
//Observations
<div class="form_sep">
@Html.LabelFor(model => model.ProductItemlist[i].MatriculationObservation)
@Html.TextBoxFor(model => model.ProductItemlist[i].MatriculationObservation, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProductItemlist[i].MatriculationObservation)
</div>
}
}
我的控制器:
[HttpPost]
public ActionResult ProcessPurchases(PurchaseRegistrationProductEditVM model)
{
Log.Instance.Info(string.Format(LogConst.CONTROLLER, "Client", "ProcessPayments", "Get"));
string test = Request.Form["ProductItemlist[0].ProductItemId"];
return RedirectToAction("Details/" + model.ClientId, "Client");
}
答案 0 :(得分:1)
经过一夜安眠后,我能够解决这个问题! :)
最后问题出在这一行:
@ Html.HiddenFor(model =&gt; model.ProductItemlist)
由于我已经存储了相同类型的列表,因此表单映射器使用的是第一个。而且由于你无法在隐藏字段中保存整个列表......它返回了一个用0个元素初始化的列表!
删除该行后,一切都像魅力一样开始工作!