开发者

ASP.NET MVC View Messages

I am using FluentValidation to validate my models, and it works awesome.

One question I have though is how do you handle messages that are not attached to a property?

Example: A customer login view. When the login is invalid I want to display a simple message.

What I did was add a property to the model, Message, and then created a validation message for that property on the view.

It works, but was looking to see what others are doing.

Update

So for simplicity, consider the following:

View Model

'Uses a FluentValidation Validator
Public Class LogonViewModel
    Public Property UserName AS String<br>
    Public Property Password AS String
End Class

View

<div id="GenericMessage">--Generic Messages Go Here--</div>
@<table border="0" cellpadding="2" cellspacing="0">
    <tr>
        <td>User Name:</td>
        <td>@Html.EditorFor(Function(x) x.UserName) @Html.ValidationMessageFor(Function(x) x.UserName)</td>
    </tr>
    <tr>
        <td>Password:</td>
        <td>@Html.EditorFor(Function(x) x.Password) @Html.ValidationMessageFor(Function(x) x.Password)</td>
    </tr>
    <tr>
        <td></td>
        <td><input type="submit" value开发者_如何学C="Logon" /></td>
    </tr>
</table>

What I want to do is have a generic message div that I can display messages in, like "Invalid Login". My question is where to I put that in the model? Do I create a property and then set it in the controller ModelState? Or should I be using ViewData?


Any reason you aren't using the ModelState for your errors?

For example, if your view model has a datetime property and a user enters something like "blah", the ModelState will automatically have that error returned when you render the view again.

That error can be retrieved in the Validation Summary...

<%: Html.ValidationSummary() %>

Or you can bind it to specific form elements.

<%: Html.ValidationMessageFor(m => m.Birthdate) %>

You can also manually add error messages to the ModelState in your controller.

// for a specific property
ModelState.AddModelError("Birthdate", "You can't use this date!")

// to show in summary
ModelState.AddModelError("", "Dates are too close!")

If you are just after a way to communicate things from your controller to your view (other than errors), then I think it's fine to add a property to your viewModel, assign it a value in your controller, and then access it in your view.

And I don't see anything wrong with doing this for errors if ModelState isn't meeting your needs.


One question I have though is how do you handle messages that are not attached to a property?

As far as I understood from that question is you do not give any hint to framework to validate the input. right? if so, do it that way.

put the following code on your view;

@Html.ValidationSummary()

and validate your input inside the post action method. if it is not valid, add the error message to view state. here is an example;

        if (captchaValid == false) {

            ModelState.AddModelError("recaptcha", "Invalid characters on securty code! Please try it again");
            return View(model);

        }


Although I think I like better the Modelstate answers what I usually do is to define things like this in my _Layout.cshtml:

@if(TempData["Error"]!=null)
{
    <div class="error">@TempData["Error"]</div>
}
@if(TempData["Warning"]!=null)
{
    <div class="warning">@TempData["Warning"]</div>
}

then I only have to assign TempData["Error"] or TempData["Warning"] in my controller.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜