Displaying an asterix when using Html.LabelFor() with a Required property - asp.net-mvc

I'm using ASP.NET MVC2 and Data Annotations.
I've decorated a property in my buddy class with the Required attribute.
Is there a way to get the Html.LabelFor() helper method to automatically display an asterix to signify that the field is required?
Only ways I can think of to do this are:
a) Extend LabelExtensions
Or
b) Manually add the asterix to the DisplayName attribute, e.g. "My Field *"
The latter is simplest, but introduces a dependency between the DisplayName and Required attributes.
Any other suggestions?

Instead of using the Html.LabelFor, you should create your own view helper which will create the label and insert the * is appropriate. You can find detailed info about how to create a new view helper (which is very easy) at http://mvcviewhelpers.codeplex.com/.

You need to create your own default templates. This article series by Brad Wilson explains how to do this:
http://bradwilson.typepad.com/blog/2009/04/dataannotations-and-aspnet-mvc.html

Related

Custom UIHint attribute

Is it possible to create a custom version of the UIHint attribute?
When my company first adopted MVC, we used a lot of Html.* helper methods. We are in the process of redesigning out MVC template to make use of the full power of MVC. One way we are doing this is with Display and Editor Templates.
However, one popular HTML extension method we had was to generate dropdowns for Enums. One of the options we had was to sort by the int value or the description or text of the EnumMember.
I would like to see about creating a EnumDropdown attribute that accepts several parameters that can customize the output of the HTML dropdown. However, I don't think it's possible to do this while still retaining the benefits of the UIHint attribute. Meaning, that I won't be able to simply call #Html.EditorFor(m => Model)
I had found that there is a System.Web.UI.IAutoFieldGenerator interface but it doesn't appear to do what I want. Any suggestions?
The newer versions of MVC have this built in now:
EnumDropDownListFor HTML Helper
The only thing UIHint does is suggest a Display or Editor template name. MVC will then add this name to the search path when looking for that template.
You can just use UIHint as is and have your generator create these for you in the correct folders and not have to customize it.

Conditional required data annotation with EditorForModel

We have a ViewModel for "create" and one for "edit." The edit inherits from create so that we're sharing common fields. We are then using an #Html.EditorForModel("User") that uses the "create" as its model.
This is for editing users, so I need the password field to be required on create, but not required on delete. Thus, the create VM has Password with [Required] decorating it, while the edit VM has password property with no decoration, and is also declared with new (public new string Password {get;set;}).
This is almost exactly similar to this question:
MVC4 Conditional Required Annotation
For my situation, it's still requiring the field I believe due to the EditorTemplate using the create object as its model instead of the edit. I tried changing it from editorformodel to:
#Html.EditorFor(model=>model, "User")
in hopes that because edit is actually a "create" due to inheritance that it would work, but it'still barking at me for not providing the required field when I post.
Is there a way to accomplish what I'm attempting, or are my options to either remove the required and handle it server-side, or split the editor template into two templates?
You can create a custom attribute to drive your check on whether the password is required or not. When the submission happens to the server your custom attribute can check to see if the you are dealing with an Update or and Insert and then invalidate the model if it needs to.
Here are some resources on creating custom attributes and custom attributes with unobstrusive jquery validation
Happy Coding!!

How to make a RequiredAttribute for create not for edit of model

I have a model containing a property of HttpPostedFileBase type, and I have created a custom validation attribute which implements RequiredAttribute to make some validations. It works perfectly when you create the model. However I don't want to make any validation when you edit the model (optional HttpPostedFileBase property when you edit the model). How do I make it?
Your attribute is simple metadata added to a property of a class. This attribute has no information about what you do with the class, i.e. if you're using this class to edit or to create a new entity.
So, the answer is that you cannot do that directly in the attribute. So the only solution is to "bypass" the attribute where you're using it, if you're editing the value. There are several alternatives:
use two different models, one for editing, the other for creating. And decorate the property only in the model for creating
remove the validation, or the error, using some code to do it: for example, you can remove the property error from the ModelState in a post action for the Edit action.
Obviously the easiest is 1. You can use a base class with all the fields, but those which have different treatement, and then inherit it for creation or edition. It's not strange to represent the same data with different models when you use MVC: a model for viewing, a model for showing the editor, a model for receiving the result of an edition... so, using several different models is not a problem at all. You can use AutoMapper or ValueInjecter to simplify moving data between entities and models.

Asp.net MVC model property meta data to specify TextBox id

With MVC, when you use something like #Html.TextBoxFor(...) the control renders using property attributes from the model for things like validation.
It also uses the class's property name as the name of the rendered HTML control. This conflicts a little with our naming conventions so I'd like to be able to control the ID a bit more.
I've added the 'htmlAttributes:' to the helper, which does what I want, but I was wondering:
How does this impact MVC's ability to instantiate the model again
when the information is posted?
Is there a way to specify the control ID using property attributes in the model class?
Thanks,
Jacques
How does this impact MVC's ability to instantiate the model again when the information is posted?
The ID attribute has absolutely no impact on anything that is posted. The ID attribute is never used when submitting a form. The only requirement is that this attribute is unique throughout your entire DOM. It is the name attribute that is used by the model binder. You cannot change this attribute anyway using the htmlAttributes because this attribute is inferred from the lambda expression used as first argument and you shouldn't need to change it anyway, otherwise you could break the way the default model binder rehydrates your view models.
Is there a way to specify the control ID using property attributes in the model class?
Yes, but it could be a little more work. You could use custom editor templates for the standard types and a custom metadata aware attribute that will pass this additional metadata information to the custom editor template. If you are interested in the implementation specifics I could provide an example but first I have to understand why you need that.

ASP.NET MVC / Linq-to-SQL classes: Can I get it to infer readable display names?

If I have a table Orders with fields CustomerID, OrderID and OrderDate, then the "Linq-to-SQL classes" generated class will be called Orders, with members called CustomerID, OrderID and OrderDate. So far so good.
However, if I then do Html.LabelFor(m => m.OrderDate) then the generated text will be "OrderDate" instead of "Order Date".
I tried using Order_Date as the field name, but that didn't work. Is there any way to get it to infer a better display name?
[I know that I can use data annotations to specify the display name explicitly, but I really don't want to do that for all my classes/members - I just want it to work by convention.]
I suggest you create your own HTML Helper for this, something like Html.MyLabelFor.
The rules to apply from here are up to you. You can simply split the word by case.
There is a solution available for your requirements contained within the answer to this question. Asp.Net MVC 2 LabelFor Custom Text.
This method takes advantage of existing MVC 2 architecture to place conventions over the entire *For rendering methods instead of one off HTML helpers and without having to re-label everything with spaced property names.
How to "DRY up" C# attributes in Models and ViewModels?
Essentially what your doing is overriding the default ConventionModelMetadataProvider behavior of MVC 2 and providing hooks for you to insert your own opinionated conventions.

Resources