简体   繁体   English

编辑器丢失帖子数据

[英]EditorFor losing data on post

I need some help with this, please! 请给我一些帮助!

I have this Model: 我有这个模型:

public class MyModel
    {

        public int Id { get; set; }

        public string Name { get; set; }

        public string Value { get; set; }
    }

I have also a EditorTemplate 我也有一个EditorTemplate

@model MyProject.Models.MyModel

<div class="form-group">
    <label class="col-md-2 control-label">@Html.DisplayFor(model => model.Name)</label>
    <div class="col-md-10">
        <input asp-for="@Model.Value" class="form-control"/>
        <span asp-validation-for="@Model.Value" class="text-danger"></span>
    </div>
</div>

The Edit View 编辑视图

@using Microsoft.AspNetCore.Mvc.ViewFeatures
@model List<MyProject.Models.MyModel>
@{
    ViewData["Title"] = "Edit";
}
<div class="spaceUnderLogo">

    <div class="row">
        <div class="col-md-12 ">
            <h2>Edit</h2>
            <form id="manageMyModel" asp-controler="MyController" asp-action="Edit" method="post" class="">
                <div class="form-horizontal">
                    <hr />

                    @Html.EditorForModel()

                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            <input type="submit" value="Save" class="btn btn-primary" />
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

And MyController has this two methods MyController有这两种方法

[HttpGet]
public async Task<IActionResult> Edit()
    {

        List<MyModel> model = await GetCurrentInfoAsync();
        return View(model);
    }


    [HttpPost]
    public async Task<ActionResult> Edit(List<MyModel> model)
    {
        // here is where I have the issue
    }

As you see I'm having an issue with the post method. 如您所见,post方法存在问题。 When I get the view I send 2 objects: {Id = 1, Name = "A", Value = "9999"} and {Id = 2, Name = "B", Value = null} 当我得到视图时,我发送2个对象:{Id = 1,名称=“ A”,值=“ 9999”}和{Id = 2,名称=“ B”,值=空}

Edit B Value property to "8888" and in the Post I'm getting the List only with the Value property like this: {Id = 0, Name = null, Value = "9999"} and {Id = 0, Name = null, Value = "8888"} 将B Value属性编辑为“ 8888”,在帖子中,我仅使用类似以下Value属性的列表:{Id = 0,名称= null,值=“ 9999”}和{Id = 0,名称= null ,Value =“ 8888”}

Any idea? 任何想法?

You can use: 您可以使用:

@model MyProject.Models.MyModel

@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.Name)

<div class="form-group">
    <label class="col-md-2 control-label">@Html.DisplayFor(model => model.Name)</label>
    <div class="col-md-10">
        <input asp-for="@Model.Value" class="form-control"/>
        <span asp-validation-for="@Model.Value" class="text-danger"></span>
    </div>
</div>    

The trick here is the: 这里的窍门是:

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

This will ensure that the form contains the value when posted, but there is no editor visible on the form. 这将确保表单在发布时包含该值,但是在表单上没有可见的编辑器。 You will need one of these hidden input for any field that is readonly for the model retrieved by a GET in any form submit. 对于以任何形式提交的GET检索的模型,对于任何只读字段,您都将需要这些隐藏输入之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM