Html literals in MVC3 model code
I have a RegisterModel view model I use for my new account registration page. Depending on the status of a registration task, the model has a relevant text message to display for the user, e.g. "Waiting for email confirmation" etc.
I currently populate this string property in the manner below, but couldn't help wondering about mixing markup and content like I do with the <p>
tags. Is there a better or m开发者_高级运维ore accepted way of doing this, besides having multiple partial views with actual HTML literals instead of model properties?
RegisterMessage = "<p>Please use the form below to create a new account.</p>";
RegisterMessage = string.Format("<p>Passwords are required to be a minimum of {0} characters in length.</p>", _membershipService.MinRequiredPasswordLength);
EDIT: I have just noticed that the <p>
tags are rendering literally, but I doubt that issue falls under the same question.
You could use a status property in the view model and in the view:
@if (Model.Status == Status.StatusA)
{
<div>Message 1</div>
}
else if (Model.Status == Status.StatusB)
{
<div>Message 2</div>
}
and if you wanted to render HTML literals in Razor without encoding them:
@Html.Raw(Model.RegisterMessage)
But the example you have shown seems like a validation error message which could also be stored in a resources file.
精彩评论