更新控制器中的视图模型数据不会反映在视图的输入字段中
我有一个博客文章编辑页面,您可以在其中保存编辑或上传图像(在单个表单中多次提交)。当您上传图像时,图像链接会附加到 TinyMCE 内容区域。
表单的字段位于视图用户控件中(与创建页面共享)。 viewpage 和 usercontrol 都继承自 BlogPost
,因此可以使用 <% Html.RenderPartial("Fields", Model); 直接传递模型。 %>
所以这是奇怪的事情;在我的控制器中,当我将图像链接附加到文本区域时,视图中的文本区域没有任何反应在
我的视图页面上,我有一个 Model.Title
标签,在用户控件中,我有用于编辑的文本框模型.标题
。
如果我更新控制器中的标签 - model.Title = "New Title"
- 更新后的模型数据会更改视图页中的标签,但不会更改用户控件中的文本框。
我的控制器是这样的:
// /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
I have a blogpost edit page where you can either save your edits or upload an image (multiple submits in a single form). When you upload an image, the image link gets appended to a TinyMCE content area.
The fields for the form are in a viewusercontrol(shared with create page). Both the viewpage and usercontrol inherit from BlogPost
so the model's being passed directly using <% Html.RenderPartial("Fields", Model); %>
So here's the weird thing; in my controller, when I append the image link to the textarea, nothing happens to the textarea in the view
On my viewpage I have a label for Model.Title
and within the usercontrol I have the textbox for editing Model.Title
.
If I update the label in the controller - model.Title = "New Title"
- the updated model data changes for the label in the viewpage but not the textbox in the usercontrol.
My Controller is like this:
// /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);
}
Any ideas as to what's causing this and how to fix it? Thanks.
- I am using 4.0 and MVC 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,这种行为是设计使然,Phil Haack 已在此处回答:
ASP.NET MVC 中可能存在表单值被替换的错误。
这里还有一篇关于此问题的博客文章:
ASP.NET MVC 的 Html 帮助程序呈现错误的值! 对于我的场景
(将图像附加到tinymce),我认为清除 ModelState 是安全的,因为我们显式附加到文本区域并且尚未进行任何验证。
Turns out that this behaviour is by design and has been answered by Phil Haack here:
Possible bug in ASP.NET MVC with form values being replaced.
There's also a blog post about this here:
ASP.NET MVC’s Html Helpers Render the Wrong Value!
For my scenario (appending an image to tinymce), I think it's safe to clear the ModelState because we're explicitly appending to a textarea and not doing any validation yet.
在调用 RenderPartial 之前,顶级视图中是否有代码会更改 Model.Title 的值?
Is there any chance that there is code in the top level view that is changing the value of Model.Title before the RenderPartial is called?