0

I've looked into this question and found questions such as:

SO-Link1 SO-Link2

So the problem seems rather common - but i really don't like the solution.

Question:

I've started digging into sources but couldn't find a convenient solution yet. Is there a built in solution to this by now using attributes?

Having to use dynamic parameters in place instead of using attributes on the model is very inconvenient.

Guess i'll see what i can achieve by modifying the ModelBinder in the meantime - there has to be some way i guess.

Update:

Why do i want to do this?

I want to reduce network traffic because properties in code may be long + nested. Imagine 200 Checkboxes x 40 bytes generated names.

I can already make my modelbinder work with aliases - however in order to fully automate it, i need the TextBoxFor etc methods to use alias names instead of the actual property names.

9
  • 3
    Why would you want to change the name attribute of a control - which would mean binding and validation will fail? And the purpose of the ModelBinder is to bind the model (i.e. when you submit), not when you generate the view (which is done by the html helpers). What are you actually trying to do? Commented Jun 17, 2015 at 11:04
  • you can change modelbinding to work through alias attributes as well. the reason is quite simple actually: unnecessary network traffic. why would i want it to generate SomePropertyInSomeClassWhichCouldBeRatherLongForNoGoodReasonWithSomeMoreNesting[0].Value. I'd rather have that be "p1[0].Value" Commented Jun 17, 2015 at 11:25
  • 2
    If your model naming conventions are really that awful, just use a view model and rename the property! A ModelBinder` has got nothing to do with rendering the name attribute. That's what the html helpers do. Commented Jun 17, 2015 at 11:29
  • @StephenMuecke No. I'm not going to change my property names to reduce network traffic. I will find a way to do this - if necessary through contributing to the MVC. I am certainly not going to modify my VIEW to achieve changes in how my MODEL is being transmitted. You should understand the reason why this makes sense rather than telling me my naming conventions are bad. I want to optimize network traffic for mobile devices, not make my whole codebase less readable to satisfy a requirement. Commented Jun 17, 2015 at 11:33
  • I mean you should be following best practice and using a view model (see What is ViewModel in MVC? ). You can make the property name what ever you want. Commented Jun 17, 2015 at 11:36

1 Answer 1

1

Personally I don't think you should be totally concerned with the size of the the name attribute's values. As long as you're using compression through something like IIS then you aren't going to be saving that much.

You can however, achieve what you're after by creating custom HTML helpers which will create the HTML markup you desire.

Example Model

public class UserModel
{
    public string FullName { get; set; }
}

Helper

public static MvcHtmlString CustomTextBoxFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string name)
{
    var fieldName = ExpressionHelper.GetExpressionText(expression);

    //
    // Pass in alias or call method to get alias here
    //
    var fullBindingName = String.IsNullOrWhiteSpace(name) ? html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(fieldName) : name;
    var fieldId = TagBuilder.CreateSanitizedId(fullBindingName);

    var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
    var value = metadata.Model;

    var tag = new TagBuilder("input");
    tag.Attributes.Add("name", fullBindingName);
    tag.Attributes.Add("id", fieldId);
    tag.Attributes.Add("type", "text");
    tag.Attributes.Add("value", value == null ? "" : value.ToString());

    var validationAttributes = html.GetUnobtrusiveValidationAttributes(fullBindingName, metadata);
    foreach (var key in validationAttributes.Keys)
    {
        tag.Attributes.Add(key, validationAttributes[key].ToString());
    }

    return new MvcHtmlString(tag.ToString(TagRenderMode.SelfClosing));
}

Call the Method in your View

   @Html.TextBoxFor(x => x.FullName)

   @Html.CustomTextBoxFor(x => x.FullName, "FName")

Output

<input id="FullName" type="text" value="Heymega" name="FullName">
<input id="FName" type="text" value="Heymega" name="FName">
Sign up to request clarification or add additional context in comments.

2 Comments

While this way certainly would be working i don't want to wrap every component we use just to get a control of the generated name. That's why i'm trying to get this done somehow using the MetadataProvider. Not done with it yet though.
@AndreasMüller Sorry this wasn't what you were after. Good luck with your coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.