我有一个博客文章编辑页面,您可以在其中保存编辑或上传图像(在单个表单中多次提交)。当您上传图像时,图像链接会附加到 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/