【问题标题】:MVC is passing old text value to controller after editMVC 在编辑后将旧文本值传递给控制器
【发布时间】:2018-10-10 11:10:30
【问题描述】:

我有一个从视图模型加载文本的文本字段,然后我想更改文本并将其发送到我的控制器。但是在我编辑了文本之后,控制器从我的文本字段中收到了旧的文本值。

我的观点:

    @model Football_Insider.ViewModels.ArticleViewModel

    @{
        ViewBag.Title = "EditArticle";
    }

<section id="breadcrumb">
    <div class="container">
        <ol class="breadcrumb">
            <li class="active"></li>
        </ol>
    </div>
</section>

    @using (@Html.BeginForm("EditArticle", "Article", FormMethod.Post, new { 
    enctype = "multipart/form-data" }))
    {
    <div class="form-group">
        @Html.LabelFor(model => model.Article.Title, htmlAttributes: new { @class = "control-label col-md-12" })
        <div class="col-md-12">
            @Html.EditorFor(model => model.Article.Title, new { htmlAttributes = new { @class = "form-control col-md-12" } })
            @Html.ValidationMessageFor(model => model.Article.Title, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Article.Content, htmlAttributes: new { @class = "control-label col-md-12" })
        <div class="col-md-12">
            @Html.TextAreaFor(model => model.Article.Content, new { @class = "form-control col-md-12", @rows = 20, @cols = 50 })
            @Html.ValidationMessageFor(model => model.Article.Content, "", new { @class = "text-danger" })
        </div>
    </div>
    @Html.Hidden("articleId", Model.Article.ArticleId)
    @Html.Hidden("title", Model.Article.Title)
    @Html.Hidden("content", Model.Article.Content)

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

【问题讨论】:

  • 你的控制器代码在哪里?
  • 为什么Model.Article.Title 有一个EditorFor 一个Hidden? (我假设这就是您所指的文本值?)
  • 您需要出示您的控制器代码(您接受的答案不正确)

标签: c# .net model-view-controller


【解决方案1】:

您需要删除@Html.Hidden("content", Model.Article.Content) 因为它会覆盖您的文本区域值

【讨论】:

  • 我知道但是如何将编辑后的值传递给我的控制器呢?
  • EditorFor 会处理这个问题
  • 谢谢你们这就是我要找的:)
  • 不,它没有。 DefaultModelBinder 绑定请求中的第一个名称/值对,并忽略具有相同名称的后续值。在任何情况下,隐藏输入用于完全不同的属性 - content(隐藏输入)与 Article.Content(文本区域) - 第二个是属性 Article,它是一个复杂对象,包含一个名为 Article 的属性)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多