ASP.NET MVC 2 中的反馈 UI 数据存储在哪里?
一般来说,我对 ASP.NET MVC 和 MVC 模式都很陌生。
对于上下文,我当前正在运行 .NET 4.0、MVC 2。
我知道 MCV 2 有许多内置功能,用于:
验证(客户端和服务器端,通过各种方式)
错误处理(通过属性和其他方法)
但是应该使用什么来返回反馈对于用户来说,这既不是错误也不是验证?
例如,我有一个简单的表单,如果找到数据则返回 csv(使用myController.base.file())。
如果未找到数据,我想按原样返回带有模型的视图以及一条消息,例如“未找到数据,请尝试不同的日期范围”
现在,
应该将这种反馈消息存储在模型本身中吗?,或者
是否有更清晰的机制?
这就是 ModelStateDictionary 的用途吗? ?
更新
只是为了澄清。我知道可能有很多方法可以完成我想要的事情,但是正确的 MVC 方法是什么。
请记住,反馈数据不是错误,我不想重定向到其他视图。
I'm really new to both ASP.NET MVC and the MVC pattern generally.
For context, I'm currently running .NET 4.0, MVC 2.
I know that MCV 2 has a bunch of built in features for:
validation (both client and server side, through a variety of ways)
error handling (via attributes and other methods)
But what should be used to return feedback to the user that is neither an error nor validation?
For example, I have a simple form that returns a csv (using myController.base.file()) IF data is found.
If data is not found, I'd like to return the View with the Model as-is plus a message, like, "no data found, try a different date range"
Now,
should that kind of feedback message be stored in the model itself?, or
is there a cleaner mechanism for this?
Is that what the ModelStateDictionary is for?
UPDATE
just to clarify. I know there may be many ways to do what I want, but what's the correct MVC way of doing it.
Keep in mind the feedback data is not an error, and I don't want to redirect to some other view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我之前曾成功使用 ModelState.AddModelError 来显示摘要消息。只需确保您使用的键不是模型字段名称即可。
注意:我根据 Steven Sanderson 的书改编了我的设计(请参阅 RulesException 上的索引)
祝你好运
I have previously used ModelState.AddModelError successfuly to show a summary message. Just make sure you use a key that is not a Model field name.
Note: I have adapted my design from Steven Sanderson's book (see the index on RulesException)
Good luck
验证错误直接源于用户对模型的操作(例如密码太短)
在家里尽可能接近模型。
通过使用某种专用的错误视图,可以更轻松地解决“未找到数据”类型的一般错误消息。
Added:
如果我理解正确的话,你更愿意坚持一种观点;无论是验证黄金还是烧毁 SQL 服务器;-)
我自己并没有疯狂地体验 MVC2,但始终保持一致并始终创建自己的模型,而不是您正在使用的(不是)ORM 可能提供的服务(Linq<->SQL, EF) 应该为您提供所需的细粒度。
如果我没记错的话,我认为 Nerddinner 可能会有所启发;如果你真的
想要尝试一下,为什么不去 深入了解 ASP.NET MVC 2 模型元数据和验证
I've gotten my MVC mainly from books, but the blogosphere has definitely started producing golden material on the more "foreign" parts of ASP.NET MVC.
My real aha moment regarding the M of MVC came while reading
Steve Michelotti's ASP.NET MVC View Model Patterns (a great source for MVC2 info all round)
Validation errors directly stemming from user actions on your model (e.g. too short a password)
is at home as close to the model as possible.
General error messages of the "no data found" kind are easier addressed through having a dedicated Error View of some sort.
Added:
If I understand you correct, you prefer to stick with one view; be it validation gold or burning SQL servers ;-)
Not madly MVC2 experienced mysef, but being consistent and always create own models, never what you might be served by what(not) ORM you're using (Linq<->SQL, EF) should give you the fine-graininess you need.
If my memory serves me correct I think the Nerddinner could be enlightening; and if you really
want to take the plunge, why don't go Deep Inside ASP.NET MVC 2 Model Metadata and Validation
I've gotten my MVC mainly from books, but the blogosphere has definitely started producing golden material on the more "foreign" parts of ASP.NET MVC.
My real aha moment regarding the M of MVC came while reading
Steve Michelotti's ASP.NET MVC View Model Patterns (a great source for MVC2 info all round)
我认为ViewModel的想法可能会消除误解。除了最简单的场景之外,您会在项目中发现不止一种类型的模型。它们可以有很多名称,但我发现这些名称很有帮助:
纯域模型(模型)
这是您对我们的数据模型的理想表示。
应用程序模型 (ViewModel)
这些模型说明了在视图中显示域模型的现实情况。它们包含特定视图特别需要的数据。在这种模型中放置状态消息之类的东西是完全可以接受的。
我会推荐 这篇富有洞察力的博客文章展示了何时、为何以及如何使用 ViewModel。
例子:
I think what might clear the air is the idea of a ViewModel. Except for the most simple scenarios, you'll find more than one kind of model in your project. They can go by many names, but I find these names helpful:
Pure domain models (Models)
This is where you have your ideal representations of our data model.
Application models (ViewModels)
These models account for the realities of displaying your domain model in a view. They contain data specifically needed for a specific view. It's perfectly acceptable to put something like a status message in this kind of a model.
I would recommend this insightful blog post which shows when, why and how to use ViewModels.
Example:
如果您正在谈论要在视图中的某个位置编码的消息,则应该将其放在模型上并让视图使用它。
如果您希望能够在整个应用程序中以相同的方式处理系统消息(例如,在窗口的顶部或侧面显示消息),您可以创建一个实用程序方法,使用特殊的键将信息放入 ViewData 中这可能会被您的母版页获取。如果您改用 TempData,则可以通过重定向保留该消息。
If you're talking about a message that you want to code for somewhere in your view, you ought to have that on your model and have your view consume it.
If you want to be able to handle system messages generally in the same way across your application (with a message at the top or side of the window, e.g.), you might create a utility method that puts the information in ViewData with a special key that could get picked up by your master page. If you use TempData instead, the message could be persisted across a redirect.