2

When I'm using the Html.EditorFor helper in my form, I wish to be able to customize the look of the tag and so I defined a EditorTemplate to be able to do just that. This is what my form looks like in my view :

@model Models.SimpleUserClass
@{
    ViewBag.Title = "Create User";
}

<div class="row">
    <div class="col-lg-6" id="FromPlaceHoler">

        @using (Html.BeginForm("CreateUser", "Users"))
        {

            <fieldset>
                <legend>Create One Way Trip</legend>

                <div class="editor-label">
                    @Html.LabelFor(model => model.UserName)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.UserName)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.UserSurname)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.UserSurname)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.UserAge)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.UserAge)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.UserGender)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.UserGender)
                </div>

                <p>
                    <input type="submit" value="Create" />
                </p>
            </fieldset>
        }
    </div>
</div>

Properties like UserName and Surname are of type String, so in my Shared folder I Created the EditorTemplates folder and created a String.cshtml file to define the way a editor for a String should look like. When running a test of the form, it seems to be working well since the input fields that is generated has appearance that I specified - problem is when I submit a form using these custom input fields, mvc cannot map my form fields to my model. The reason for this is the properties that the tag needs to support this binding to models. Here follows an example:

Here is my code in the String.cshtml EditorTemplate :

<input type="text" class="form-control">

and when using the default EditorFor without customization it generates the following markup:

<input data-val="true" data-val-required="The User Name field is required." id="UserName" name="UserName" type="text" value="" />

As we can see above, my code does not have the 'id' and 'name' properties that correlate to the name of my properties in my class and which is used to bind values to my class. So how to I change the code in the String.cshtml to include these properties and set their values dynamically? since it should be used for various properties in my class?

1
  • 1
    You don't need an EditorTemplate for such a simple thing. Why don't you just assign the class to a Html.TextBoxFor? Commented Apr 7, 2015 at 19:26

2 Answers 2

5

You can use this:

@model string

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
                new
                {
                    @class = "form-control",
                    placeholder = ViewData.ModelMetadata.Watermark 
                                  ?? ViewData.ModelMetadata.DisplayName 
                                  ?? ViewData.ModelMetadata.PropertyName
                })

Remove placeholder, if you don't want it.

Re: placeholder, this is a hint to the user of what can be entered in the input. You can decorate your model properties with annotations:

[Display(Name = "Client")]
public int ClientId { get; set; }

[Display(Prompt = "Please enter mailing Name")]
public string MailingName { get; set; }

If you use something like this, then your inputs will have placeholder attribute with values taken from these annotations, in the order specified in the editor template (Prompt (watermark), Name, then name of the property).

Sign up to request clarification or add additional context in comments.

1 Comment

Wow this is great! Since I'm new to mvc this is really a helpful piece of code you shared here. Just one question, what is the placeholder you refer to, where in the markup does it get rendered?
0

Instead of using an EditorTemplate for String, just use a TextBoxFor like this:

@Html.TextBoxFor(model => model.UserName, new { @class = "form-control" })

5 Comments

This is the correct answer, however I disagree with your analysis on why it is not a good idea to create an editor template for type String. I don't see anything wrong with having a default custom String editor template. If you need to have multiple editor templates for type String later down the road (for example, you want a <textbox> instead of <input type='text'>) then you can easily use [UIHint()] attribute to specify which template to use.
Well, wouldn't it better to use [UIHint()] in cases when you need a custom template? I see your point, but I can't think of any real case scenario in which a default template for the type String is needed.
such as in this example, the person would be able to include the <div class="editor-field"> wrapper inside of the string editor template rather than typing/copy-paste the wrapper for each field.
I do not understand the the argumentation that the type string is too general for templates. To me it looks like a big benefit when i do not have to care to distinct between EditorFor and TextBoxFor and do not have to copy "form-control" everywhere. Could you further explain maybe?
@DiegoFrehner If you want to use an EditorTemplate to add a class, you're absolutely right. I removed that from my answer.

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.