编辑因丢失帖子数据

时间:2018-06-05 15:37:44

标签: c# .net asp.net-mvc post asp.net-core-mvc

我需要一些帮助!

我有这个型号:

import discord

client = discord.Client()

@client.event
async def on_server_join(server):
    await client.send_message(server.owner, "I'm ready!")

client.run("YOUR TOKEN")

我还有一个EditorTemplate

public class MyModel
    {

        public int Id { get; set; }

        public string Name { get; set; }

        public string Value { get; set; }
    }

编辑视图

@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>

MyController有这两种方法

@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>

如您所见,我遇到了post方法的问题。当我得到视图时,我发送了2个对象:{Id = 1,Name =&#34; A&#34;,Value =&#34; 9999&#34;}和{Id = 2,Name =&#34; B& #34;,Value = null}

将B值属性编辑为&#34; 8888&#34;并且在Post中我只使用Value属性获取List,如下所示: {Id = 0,Name = null,Value =&#34; 9999&#34;}和{Id = 0,Name = null,Value =&#34; 8888&#34;}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用:

@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>    

这里的诀窍是:

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

这将确保表单在发布时包含值,但表单上没有可见的编辑器。对于GET以任何形式提交检索的模型,只读取任何字段,您将需要其中一个隐藏输入。