开发者

ASP.Net MVC 3 EditorTemplate for DateTime Fields Error

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 开发者_C百科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.


Actually it's @model with lowercase m:

@model DateTime?
 ^

instead of:

@Model DateTime?


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");


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜