简体   繁体   English

ASP.Net MVC错误验证-将自定义视图模型传递给视图时如何显示验证消息

[英]ASP.Net MVC Error Validation - How to display validation message when passing a custom view model to a view

I have been adding error and business validation to my app, and when I test using a view that was strongly typed to one model, let's say locations, I get the validation summary as well as the validation messages for each field that didn't pass, plus my css highlights the appropriate field as expected... 我一直在向应用程序添加错误和业务验证,当我使用强类型化到一个模型的视图进行测试时,假设位置,我会收到验证摘要以及未通过的每个字段的验证消息,加上我的CSS会按预期突出显示适当的字段...

...when I try this with a view that's strongly typed to a custom view model, let's say I passed it location - so the user can enter a new one, as well as ...当我使用强类型化为自定义视图模型的视图尝试此操作时,假设我将其传递给了位置-这样用户就可以输入新的

IEnumerable<Location> locations

, so it will list out all of the existing locations below the new location form. ,因此它将在新位置表格下方列出所有现有位置。 When I do this I get the validation summary, but the message for each field is not displaying, nor is the css applying the * and the highlighting to each incorrect field. 当我这样做时,我得到了验证摘要,但是没有显示每个字段的消息,cs也没有将*和突出显示应用于每个不正确的字段。

I am using Linq to SQL, so I have added all of my validation as partial classes, so in this case all of the validation comes from the partial class location, which to my understanding will compile with the designer file that linq to sql created and add my business validation. 我使用的是Linq to SQL,因此我将所有验证都添加为部分类,因此在这种情况下,所有验证都来自部分类位置,据我所知,它将与linq to sql创建的设计器文件一起编译,添加我的业务验证。 Is this problem happening because I am not passing the view MyApp.Models.Location, and instead passing it MyApp.Models.MyCustomViewModel? 是否发生此问题,因为我没有传递MyApp.Models.Location视图,而是传递了MyApp.Models.MyCustomViewModel视图? if so, what's my best approach so that I can use just one form for create and list? 如果是这样,我最好的方法是什么,这样我就可以只使用一种形式进行创建和列出?

I was able to get this working - I changed the return value in my POST method to return 我能够使它正常工作-我将POST方法中的返回值更改为return

return View(new MyCustomViewModel(location) { });

instead of 代替

return RedirectToAction(new MyCustomViewModel(location) { });

hope this helps if anyone stumbles across the same problem I did - which was my own misunderstanding of how View works differently than RedirectToAction. 希望这对任何遇到我遇到的相同问题的人有所帮助-这是我自己对View与RedirectToAction的工作方式不同的误解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关于模型验证ASP.NET Core MVC的自定义错误消息 - Custom error message on model validation ASP.NET Core MVC 如何使用Asp.NET 4.5和Razor视图(非MVC)添加自定义验证消息 - How to add custom validation message using Asp.NET 4.5 and Razor view (Not MVC) ASP.NET MVC3 Automapper Viewmodel / Model View验证 - ASP.NET MVC3 Automapper Viewmodel/Model View validation 在ASP.Net MVC的视图模型中验证嵌套模型 - Validation of nested models in view model in ASP.Net MVC 在父视图中显示子节点的验证 asp.net mvc 3 - display validation of child in parent view asp.net mvc 3 传递视图 model 以在 ASP.NET MVC 中查看时出错 - Error while passing view model to view in ASP.NET MVC ASP.NET Core MVC 自定义验证不会显示错误信息,仍然提交表单 - ASP.NET Core MVC custom validation will not display error message and still submits form 当 ASP.NET MVC 中的 Model.isvalid 为 false 时,如何将验证消息重定向并显示回部分视图 - How to redirect and show validation messages back to a partial view when Model.isvalid is false in ASP.NET MVC 如何在 ASP.NET MVC5 中的视图 model 中进行字符串“不喜欢”验证? - How to do string “not like” validation in view model in ASP.NET MVC5? Asp.net core 如何对视图组件进行模型验证 - Asp.net core how to do model validation on View Components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM