3

Why does EditorFor renders different classes and input types for byte and short, as shown here:

<div class="form-group">
    <input class="text-box single-line" data-val="true" 
        data-val-number="The field Num Year / Period must be a number."
        id="NumYear_Period" name="NumYear_Period" type="number" value="" />
</div>

<div class="form-group">
    <input class="form-control" data-val="true" 
        data-val-number="The field Start Year must be a number." 
        id="Start_Year_Period" name="Start_Year_Period" type="text" value="" />
</div>

Where "NumYear_Period" is a Nullable Byte and "Start_Year_Period" is a Nullable Short as:

    [Display(Name = "Num Year / Period")]
    public Nullable<byte> NumYear_Period { get; set; }

    [Display(Name = "Start Year")]
    public Nullable<short> Start_Year_Period { get; set; }

The Create.cshtml view contains:

<div class="form-group">
    @Html.EditorFor(model => model.NumYear_Period)
</div>
<div class="form-group">
    @Html.EditorFor(model => model.Start_Year_Period)
</div>

I have no EditorTemplates present, so why!!

Using Bootstrap, Visual Studio 2013 Update 1, MVC 5.1.1, .Net 4.5, Razor 3.1.1

  • +1 I wonder if this is a bug in the `HtmlHelper` and it has not been updated to account for `byte`s. After all, I don't think I can remember when I last asked a user to put 1s and 0s into the browser and by using a `byte`, in your view model that's exactly what you're asking them to do. I would use a `Short` in your view model because you're asking the user to enter a number, not a series of 1s and 0s. – Dr Rob Lang Feb 19 '14 at 12:47
  • Well, it is more of a range constrict, since the model is governed by the Database model through the Entity Framework. That is, the database tables were already created, or rather inherented, and the model created as Database First. – Anders Jansson Feb 19 '14 at 12:53
  • I see. I think most people use the View Model for properties that go on the screen whereas a Domain Model is the representation of the data in the database. The Domain Model of an entity tends to be much larger, whereas a View Model is only that data you want to use on the screen. Therefore, the two are usually disconnected and some mapping must occur. – Dr Rob Lang Feb 19 '14 at 14:10

1 Answers1

2

It renders differently because there is no specific template for the type short or System.Int16 in the private collection of _defaultEditorActions defined in System.Web.Mvc.Html.TemplateHelpers. It has only defaults for:

    "HiddenInput",
    "MultilineText",
    "Password",
    "Text",
    "Collection",
    "PhoneNumber",
    "Url",
    "EmailAddress",
    "DateTime",
    "Date",
    "Time",
    typeof(byte).Name,
    typeof(sbyte).Name,
    typeof(int).Name,
    typeof(uint).Name,
    typeof(long).Name,
    typeof(ulong).Name,
    typeof(bool).Name,
    typeof(decimal).Name,
    typeof(string).Name,
    typeof(object).Name,

As you already stated that you have no EditorFor templates there is no otherway for the MVC framework to render a default input tag for you.

To have a specific renderig for a short datatype add a file Int16 to the EditorTemplates folder under your view folder or under the Shared folder with hte following content:

@model short

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @type = "number" }) 

This will render short types from your models as

<input ..... type="number" ... />

Alternatively you could decorate your model property with an UIHint like so:

[Display(Name = "Start Year")]
[UIHint("Int32")]
public Nullable<short> Start_Year_Period { get; set; }

which basically instructs the TemplateHelper to use the template for the int type (or System.Int32 in full)

Community
  • 1
  • 1
rene
  • 41,474
  • 78
  • 114
  • 152