如何将修改后的数据对象列表传递给控制器?

问题描述 投票:0回答:1

我是新人,所以有些事情对我来说不是很清楚,但我有一个列表,其中我在 html 中显示的数据工作正常,但如果我尝试修改字段并尝试保存,一旦它到达控制器,我看到只有 GridLanguageList 为空,所以我无法保存对视图所做的更改。

在html中我以这种方式显示数据:

@if (Model.GridLanguageList != null && Model.GridLanguageList.Count > 0)
            {
                for (int i = Model.GridLanguageList.Count() - 1; i >= 0; i--)
                {
                    //var currentItem = Model.GridLanguageList[i];
                    @Html.HiddenFor(m => m.GridLanguageList[i].IdCaseLanguage)
                    @Html.HiddenFor(m => m.GridLanguageList[i].IdCase)
                    @Html.HiddenFor(m => m.GridLanguageList[i].IdLanguageIso)
                    @Html.HiddenFor(m => m.GridLanguageList[i].FormatType)
                    @Html.HiddenFor(m => m.GridLanguageList[i].PublicationDate)
                    @Html.HiddenFor(m => m.GridLanguageList[i].RevisionDate)
                    @Html.HiddenFor(m => m.GridLanguageList[i].ModPdfDate)
                    <div class="col-md-6">
                        <div class="panel panelIese">
                            <div class="panel-heading bg-danger  text-white">
                                <div class="col-md-11">
                                    @Model.GridLanguageList[i].DescripLanguageIso
                                </div>
                            </div>
                            <div id="panelSpanish" class="panel-body">
                                <div class="form-group">
                                    @Html.Raw("Title test:")
                                    @Html.TextAreaFor(m => m.GridLanguageList[i].Title, 3, 300, new { @class = "form-control form-control-sm", maxlength = "250" })
                                </div>
                                <div class="form-group">
                                    @Html.LabelFor(m => m.SubTitle)
                                    @Html.TextBoxFor(m => m.GridLanguageList[i].SubTitle, new { @class = "form-control form-control-sm", maxlength = "360" })
                                </div>
                                <div class="row">
                                    <div class="form-group col-md-4">
                                        @Html.LabelFor(m => m.PublicationDate_, new { style = "display: block;" })
                                        @(Html.Kendo().DatePicker().Name("PublicationDate_"+ i + "").Format(Constants.DateFormat).HtmlAttributes(new { @class = "dateForm" }).Value(Model.GridLanguageList[i].PublicationDate))
                                    </div>
                                    <div class="form-group col-md-4">
                                        @Html.LabelFor(m => m.RevisionDate_, new { style = "display: block;" })
                                        @(Html.Kendo().DatePicker().Name("RevisionDate_" + i + "").Format(Constants.DateFormat).HtmlAttributes(new { @class = "dateForm" }).Value(Model.GridLanguageList[i].RevisionDate))
                                    </div>
                                    <div class="form-group col-md-4">
                                        @Html.LabelFor(m => m.Pages)
                                        @Html.TextBoxFor(m => m.GridLanguageList[i].Pages, new { @class = "form-control", maxlength = "5" })
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="form-group col-md-6">
                                        @Html.LabelFor(m => m.GridLanguageList[i].FormatType)
                                        @Html.DropDownListFor(m => m.GridLanguageList[i].FormatType, Model.FormatTypesList, new { @class = "form-control form-control-sm" })
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                }
            }

目前在控制器中,这就是我所做的:

public async Task<PartialViewResult> Detail(MaterialDetailViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                Logger.Info(string.Format(Messages.Logger_StartSaveModel, Literals.Materials_Detail, viewModel.IdMaterial.ToString(), ControllerKeys.Material), User.Identity.Name, Session.SessionID);

                if (viewModel.OperationDone == Actions.Save || viewModel.OperationDone == Actions.SaveFile || viewModel.OperationDone == Actions.InitialChange || viewModel.OperationDone == Actions.NewLanguageIso)
                {

                    if (viewModel.OperationDone == Actions.InitialChange)
                    {
                        viewModel = viewModel.ChangeInitial();
                    }

                    var selectedLanguageIsoId = viewModel.NewLanguageIso;

                    if (viewModel.OperationDone == Actions.NewLanguageIso )
                    {
                        var newCaseLanguage = new CaseLanguage(viewModel.IdMaterial, selectedLanguageIsoId, User.Identity.Name);
                        viewModel.GridLanguageList = _apiCases.SaveCaseLanguage(newCaseLanguage).ToViewModel();
                    }
                }
            }
        }

我已经用我所做的新更改编辑了问题,我看到一些字段被修改并发送到控制器,但我发现并非所有字段都是如此,我不知道为什么。具体来说,所有 TextAreaFor 或 TextBoxFor 字段都会被通知,如果它们被修改,它们会正确到达控制器。

但是我也遇到过这样的情况

@Html.DropDownListFor(m => m.GridLanguageList[i].FormatType, Model.FormatTypesList, new { @class= "form-control form-control-sm" })

在这种情况下,通知列表选择器的值是 1,应该显示“文本”,但它总是在选择器“视频”中显示,它的值是 2,即使我修改它并将其发送到我总是看到控制器到达 1,也就是说,它没有给我我应该修改的另一个元素的值。

还有其他案例,例如:

@(Html.Kendo().DatePicker().Name("RevisionDate_" + i + "").Format(Constants.DateFormat).HtmlAttributes(new { @class = "dateForm" }).Value(Model.GridLanguageList[i].RevisionDate))

对于日期,它们可以到达空或从 BDD 通知,在这些情况下,我建议它们到达 null,但是如果我尝试使用日期选择器选择日期并将其发送到控制器进行更新,这些字段总是到达null 或者如果有通知值,尽管我使用日期选择器更改了它,但它仍然与到达时相同。

我已经检查过所有字段都被称为相同的,具有相同的数据类型,我已经调试过,我已将字段设置为隐藏以查看是否会改变,但老实说我不知道为什么有些字段做得很好而其他人什么也不做。我有点疯狂

任何建议或帮助对我来说都非常有用,但我有点卡住了。

html .net asp.net-mvc kendo-ui
1个回答
1
投票

尝试删除 currentItem 并直接使用 GridL 对象

@Html.TextAreaFor(m => m.GridLanguageList[i].Title, 3, 300, new { @class = "form-control form-control-sm", maxlength = "250" })
            @Html.TextBoxFor(m => m.GridLanguageList[i].SubTitle, new { @class = "form-control form-control-sm", maxlength = "360" })

剑道日期选择器

    @(Html.Kendo().DatePicker().Name("GridLanguageList[" + i + "].PublicationDate").Format(Constants.DateFormat).HtmlAttributes(new { @class = "dateForm" }).Value(Model.GridLanguageList[i].PublicationDate))
    @(Html.Kendo().DatePicker().Name("GridLanguageList[" + i + "].RevisionDate").Format(Constants.DateFormat).HtmlAttributes(new { @class = "dateForm" }).Value(Model.GridLanguageList[i].RevisionDate))
© www.soinside.com 2019 - 2024. All rights reserved.