【问题标题】:ASP.Net Post not workingASP.Net Post 不工作
【发布时间】:2018-05-15 07:26:25
【问题描述】:

我的 Controller 中的 Edit 方法有问题。提交编辑表单时,answer 参数始终为空。

如何才能让编辑方法中的 SmallTalkAnswer 将其写回数据库?

控制器包含 ms 文档中的编辑方法:

  [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(SmallTalkAnswer answer)
    {
        var a = ModelState.IsValid;
        _smallTalkAnswerRepository.Update(answer);
        return RedirectToAction("Index");

    }

视图由visual studio生成:

@model ChatBotAdminCenter.Models.SmallTalkAnswer

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>SmallTalkAnswer</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.PartitionKey, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PartitionKey, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PartitionKey, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.RowKey, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.RowKey, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.RowKey, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Timestamp, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Timestamp, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Timestamp, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ETag, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ETag, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ETag, "", new { @class = "text-danger" })
            </div>
        </div>



        <div class="form-group">
            @Html.LabelFor(model => model.Answer, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Answer, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Answer, "", new { @class = "text-danger" })
            </div>
        </div>

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

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

型号:

public class SmallTalkAnswer : TableEntity
    {
        public SmallTalkAnswer() { }
        public SmallTalkAnswer(string hash, string categoryId)
        {
            this.PartitionKey = hash;
            this.RowKey = categoryId;
        }

        public string Hash => PartitionKey;
        public string CategoryId => RowKey;
        public string Answer { get; set; }
    }

【问题讨论】:

  • 您有名为Answer 的模型类吗?您是否尝试将参数名称更改为其他名称?
  • 感谢@TetsuyaYamamoto - 这就是问题所在。

标签: asp.net post null


【解决方案1】:

您的模型上似乎有一个名为 Answer 的属性:

public string Answer { get; set; }

动作方法参数名与上面定义的属性名类似:

public ActionResult Edit(SmallTalkAnswer answer)

AFAIK,当在 POST 操作方法中引用模型时,此设置可能会导致默认模型绑定端混淆,因此它包含空值。

尝试将 POST 操作方法中视图模型的参数名称更改为您选择的另一个名称(例如 modelsmallTalkAnswer,请避免重复使用属性名称作为视图模型参数):

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(SmallTalkAnswer model)
{
    // other stuff
    _smallTalkAnswerRepository.Update(model);
    // other stuff
}

类似问题:viewmodel returns null on postback mvc 5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-03
    • 2015-11-21
    • 2014-05-07
    • 2012-04-12
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    相关资源
    最近更新 更多