asp.net - 更新 Controller 中的 View 模型数据不会反射(reflect)在 View 的输入字段中

标签 asp.net asp.net-mvc viewmodel

我有一个博客文章编辑页面,您可以在其中保存编辑或上传图像(在单个表单中多次提交)。当您上传图像时,图像链接会附加到 TinyMCE 内容区域。

表单的字段位于 View 用户控件中(与创建页面共享)。 viewpage 和 usercontrol 都继承自 BlogPost因此模型直接使用 <% Html.RenderPartial("Fields", Model); %> 传递

所以这是奇怪的事情;在我的 Controller 中,当我将图像链接附加到文本区域时, View 中的文本区域没有任何反应

在我的 View 页面上,我有一个标签 Model.Title在用户控件中,我有用于编辑的文本框 Model.Title

如果我更新 Controller 中的标签 - model.Title = "New Title" - 更新后的模型数据会更改 View 页中的标签,但不会更改用户控件中的文本框。

我的 Controller 是这样的:

// /edit/{id}
public ViewResult Edit(int id, BlogPost model, string submit)
    {
        if (ModelState.IsValid)
        {
            switch (submit)
            {
                case "Upload":
                    var files = UploadFiles(Request.Files); // uploading works

                    model.Content += files[0].Link; // model is updated but not cascaded at runtime
                    model.Title = "Test"; // Force a title change to reproduce the issue
                    return View(model);

                default:
                    repository.Update(model);
                    break;
            }
        }

        return View(model);
    }

关于造成这种情况的原因以及如何解决它有什么想法吗?谢谢。

  • 我使用的是 4.0 和 MVC 2

最佳答案

事实证明,这种行为是设计使然,Phil Haack 已在此处回答:

Possible bug in ASP.NET MVC with form values being replaced.

这里还有一篇关于此的博客文章:

ASP.NET MVC’s Html Helpers Render the Wrong Value!

对于我的场景(将图像附加到tinymce),我认为清除 ModelState 是安全的,因为我们显式附加到文本区域并且尚未执行任何验证。

关于asp.net - 更新 Controller 中的 View 模型数据不会反射(reflect)在 View 的输入字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3568243/

相关文章:

c# - ASP.NET MVC3 和服务器端验证

c# - ASP.NET MVC 和 C# : pass a viewmodel to controller

javascript - 上传视频时出现内部服务器错误。但上传图片工作正常。可能会出现什么问题?

asp.net-mvc - CustomErrors web.config 设置和 azure 应用洞察日志记录之间有什么关系?

c# - 为什么我的 ActionLink 无法正确生成?

c# - ViewModel 不在数据网格中显示元素

c# - 在 Controller 的 MVC **外部**中模拟 HttpContext

c# - 在 ASP.net 中使用 C# 进行套接字编程

c# - ASP.NET Core 等效于 ASP.NET MVC BeginExecuteCore

c# - 在发布 :end event 中获取选择发布的语言