Question

I am trying to make a form that lets the user choose a date range to view some stats. I can't get the datetime to show up as just the date in the textboxes unless I am using the 'Alternate DateTime Editor Template I Tried' but then I don't get it hooked up to the form because it doesn't have an id or name attribute.

What am I doing wrong?

DateTime Editor Template

@inherits System.Web.Mvc.WebViewPage<DateTime>
@Html.TextBoxFor(p => p, new { @class = "datepicker span2" })

Alternate DateTime Editor Template I Tried

@inherits System.Web.Mvc.WebViewPage<DateTime>
<input class="datepicker span2" type="text" value="@Model.ToShortDateString()" />

My Model

public class ManageStatsModel
{
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime MinDate { get; set; }

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime MaxDate { get; set; }
}

Markup on Form

@using (Html.BeginForm("Stats", "Manage", FormMethod.Post, new { @class = "form-inline" }))
{
    @Html.EditorFor(p => p.MinDate)
    <span> - </span>
    @Html.EditorFor(p => p.MaxDate)
    <button type="submit">Go</button>
}
Was it helpful?

Solution

Try:

@inherits System.Web.Mvc.WebViewPage<DateTime>
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "datepicker span2" })

The TextBox helper will build the appropriate name from the model/template hierarchy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top