Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP MVC 5发布后的空模型列表_C#_Asp.net Mvc_Forms - Fatal编程技术网

C# ASP MVC 5发布后的空模型列表

C# ASP MVC 5发布后的空模型列表,c#,asp.net-mvc,forms,C#,Asp.net Mvc,Forms,这件事我已经讨论了好几次了,如果可能的话,我希望有一双“新鲜”的眼睛来看看这件事 在我的应用程序中,我有一个表单,其中我使用了一个带有一些基本类型和两个列表的模型。我也在使用剑道格网 我的问题是,当用户提交时,模型正常到达,但两个列表中的一个返回0个元素。。。!(从不为空) 到达ok的列表就是我在剑道网格中创建的列表 返回空的列表是在部分视图中生成的列表ProductItemlist(也尝试不使用部分视图) 问题是,在控制器上,如果我这样做: 字符串测试=请求。表单[“ProductItemli

这件事我已经讨论了好几次了,如果可能的话,我希望有一双“新鲜”的眼睛来看看这件事

在我的应用程序中,我有一个表单,其中我使用了一个带有一些基本类型和两个列表的模型。我也在使用剑道格网

我的问题是,当用户提交时,模型正常到达,但两个列表中的一个返回0个元素。。。!(从不为空)

到达ok的列表就是我在剑道网格中创建的列表

返回空的列表是在部分视图中生成的列表ProductItemlist(也尝试不使用部分视图)

问题是,在控制器上,如果我这样做:

字符串测试=请求。表单[“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; }

}
型号:ClientProductPurchaseRegistryCommissionEditVM

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" })

        }

睡了一个好觉后,我终于解决了这个问题!:)

最后,问题出在这一行:

@Html.HiddenFor(model=>model.ProductItemlist)

因为我已经存储了一个相同类型的列表,表单映射者使用了他第一个得到的列表。既然你不能把整个列表保存在隐藏的字段里。。。它返回了一个用0个元素初始化的列表


删除该行后,一切都开始像一个魅力

您显示的代码看起来应该绑定到集合。通常,不为null但包含项目的集合意味着您发回了基于非零的索引器(即已删除集合中的第一个项目),但如果您使用
Request.Form[“ProductItemlist[0].ProductItemId”]
获取值,则这不是问题所在。其他常见原因是属性没有设置器或不是公共的。同样,你的代码看起来还不错。这里真正的问题是,你到底为什么有那么多隐藏字段。应使用仅包含视图中所需属性的视图模型。呈现大量额外的html,然后原封不动地发回,只会降低应用程序的性能。一个标识集合中项目ID的隐藏输入就是应该呈现的所有内容。我可以执行查询并替换所有隐藏字段。睡了一个好觉后,我终于解决了这个问题!:)最后的问题是这一行:@Html.HiddenFor(model=>model.ProductItemlist)。因为我已经存储了一个相同类型的列表,表单映射者使用了他第一个得到的列表。既然你不能把整个列表保存在隐藏的字段里。。。它返回了一个包含0个元素的列表!非常感谢你的帮助,斯蒂芬!
@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&nbsp;@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");

}