Reputation: 21
I've been around this for a couple of time and I would love if possible for a couple of "fresh" pair of eyes to look at this.
In my app I have a form where I use a model with some primitive types and two lists. Also I'm using a Kendo grid.
My problem is that when the user does Submit, the model arrives ok but one of the two lists returns with 0 elements...! (never null)
The list that arrives ok is the one I'm creating in the Kendo Grid.
The list that returns empty is List ProductItemlist, that is generated in the partial view (also tried not using the partial view).
The thing is, on the controller if I do:
string test = Request.Form["ProductItemlist[0].ProductItemId"],
I get the values I want, so the problem I think must be in the mapping.
Nevertheless I'm failing in discovering it....
What's happenning...? Thanks in advance for any help!
My Model:
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; }
}
My View:
@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" })
}
Partial View:
@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>
}
}
My Controller:
[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");
}
Upvotes: 1
Views: 3898
Reputation: 21
After a good night sleep I was able to solve this issue! :)
In the end the problem was this line:
@Html.HiddenFor(model => model.ProductItemlist)
Since I was already storing a list of the same type the form mapper used the first he got. And since you can't save entire lists in hidden fields... it returned a list initialized with 0 elements!
After deleting the line everything started working like a charm!
Upvotes: 1