Asp.Net Mvc - 渲染部分视图 - 管理错误
我得到了一个 aspx 页面调用 ListArticles,其中包含以下代码:
<% Html.RenderPartial("Create", new Models.Article()); %>
Create 是一个部分视图 (Create.ascx)。
在我的控制器中,我得到了这样的结果:
if (!ModelState.IsValid) {
return View();
}
所以问题是由 return View();
生成的视图无法呈现良好的视图。它应该渲染 ListArticles 视图,同时突出显示 Create 部分视图中的错误,但它只显示 Create.ascx 视图。
有办法处理吗?
I got an aspx page call ListArticles with the following code :
<% Html.RenderPartial("Create", new Models.Article()); %>
Create is a partial view (Create.ascx).
In my controller, I got something like this :
if (!ModelState.IsValid) {
return View();
}
So the problem is that the view generated by return View();
doesn't render the good view. It's should render the ListArticles view while highlighting errors in the Create partial view but it's only show the Create.ascx view.
Is there a way to handle that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于这种情况,我建议您在视图中嵌入一个表单,该表单需要发布并显示错误,您使用 Ajax.BeginForm 而不是部分视图。
部分视图更适合展示场景。
I suggest for this situation which you embed a form in your View which need to post and show the errors you use Ajax.BeginForm instead of partial views .
Partial Views is more suitable for showing scenarios.
您确定已提供所有代码吗?
在您的控制器中,您返回一个视图,但不向其传递模型。
所以你需要返回视图(文章)或类似的东西。发生错误时,您仍然需要返回最初用于渲染视图的集合或模型。
Are you sure you've provided all the code?
in your controller you're returning a view but not passing a model to it.
so you need Return View(Articles) or something like that. On error you still need to return the collection, or model, that you used to render the view in the first place.
您的操作名称可能为“创建”,这就是它仅显示的原因
创建.ascx视图。
尝试放置以下代码代替
您应该在 Create.ascx 中包含 Html.ValidationMessage() 以查看验证错误
You may have the action name as "Create", so thats the reason it is showing only
Create.ascx view.
Try putting the following code instead
You should have Html.ValidationMessage() in your Create.ascx to see the validation errors