ASP.Net MVC 3 EditorTemplate for DateTime Fields Error
Asked Answered
B

4

6

This code was converted from some ASP.Net MVC 2 code in this tutorial:
MVC 2 Editor Template with DateTime

It is a custom EditorTemplate for DateTime fields stored as 'EditorTemplates/DateTime.cshtml'.

@Model DateTime?
@Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" })

However I get the following error when using @Html.EditorFor(model => model.NewAbsence.StartDate):

CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'TextBox' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

I've seen some similar posts on here which mention casting the parameter of the EditorFor method, however I cannot seem to get this to work in my example.

Could someone please point out what I will need to change in my code. Thanks.

Babylonia answered 14/2, 2011 at 13:4 Comment(1)
The View is actually based on a ViewModel which contains custom data for this form. The form is a Create form, so model.NewAbsence is of type Absence which has default values. It looks like the partial EditorTeplate is being accessed so the type there would be DateTime??Babylonia
B
11

Actually it's @model with lowercase m:

@model DateTime?
 ^

instead of:

@Model DateTime?
Bite answered 14/2, 2011 at 21:49 Comment(4)
@Banford, never trust Intellisense :-)Bite
Ok so I checked my other views and they're all lowercase. Changing it to lowercase has highlighted that section in yellow. But now the code is erroring on the second line in the EditorTemplate. Stating that: CS0103: The name 'model' does not exist in the current context @Html.TextBox("", (model.HasValue ? model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" })Babylonia
yeah I guess the intellisense on a fairly new technology is not going to be 100% reliable. :) Good advice.Babylonia
@Banford, use a capital M to refer to the actual model instance in the call to Html.TextBox. Lower-case is a Razor keyword for specifying the model type. Upper-case refers to the property on the view's base class (which is WebViewPage by default).Voronezh
M
4

So to sort of summarize what people are saying, and make it a bit more generic. If your view is declaring that it accepts dynamic models:

@model dynamic

Then things like extension methods will not be able to infer the types of arguments passed to them. Here are two examples (using Razor because it's awesome):

@Html.TextBox("myTextBoxName", Model.MyTextBoxValue)
@Html.DropDownList("myDropDownName", Model.MySelectList))

In these cases, the engine doesn't know what types Model.MyTextBoxValue or Model.MySelectList are, therefore it can't figure out what overloads of the extension methods to compile. So you just help it along with some strong typing:

@Html.TextBox("myTextBoxName", (string)Model.MyTextBoxValue)
@Html.DropDownList("myDropDownName", (SelectList)Model.MySelectList))

By the way, just to stop people from potentially pulling out their hair, that SelectList has to be properly instantiated with something like:

var items = List<SelectListItem>();
...
new SelectList(items, "Value", "Text");
Mikey answered 24/2, 2011 at 21:20 Comment(0)
B
0

As a temporary work around I am using:

<div class="editor-field date-field">
    @Html.EditorFor(model => model.NewAbsence.StartDate)
    @Html.ValidationMessageFor(model => model.NewAbsence.StartDate)
</div>

Then using the jQuery selector:

 $(".date-field > input").datepicker({
                showOn: "button",
                buttonImage: "*pathtoimage*"
            });

To apply the date picker to the input tags within the 'date-field' div. However this still doesn't format the date value how I want it to display initially, and cuts out the editor template entirely.

Babylonia answered 14/2, 2011 at 16:1 Comment(0)
J
0

The error message comes from your textbox statement. In a template, this becomes a dynamic expression, and .Net doesn't know how to type the Model properties.

@Html.TextBox("", (string)(Model==null ? Model.Value.ToShortDateString() : string.Empty), new { style = "width: 10em;", @class="datefield" })

Explicitly cast your date value as string, and the dynamic expression has the information it needs. I also had a problem with the .HasValue property, but that wasn't the point of your question.

Janae answered 21/2, 2011 at 2:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.