【问题标题】:DateTime EditorFor Template - Setting id and name attributes?DateTime EditorFor Template - 设置 id 和 name 属性?
【发布时间】:2012-05-22 00:53:29
【问题描述】:

我正在尝试制作一个表单,让用户选择一个日期范围来查看一些统计信息。除非我使用“我尝试过的备用日期时间编辑器模板”,否则我无法将日期时间显示为文本框中的日期,但是我没有将它连接到表单,因为它没有id 或 name 属性。

我做错了什么?

日期时间编辑器模板

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

我尝试过的备用日期时间编辑器模板

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

我的模特

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; }
}

表单上的标记

@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>
}

【问题讨论】:

    标签: c# asp.net-mvc-3 editorfor


    【解决方案1】:

    试试:

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

    TextBox 助手将从模型/模板层次结构中构建适当的名称。

    【讨论】:

    • 谢谢!令人惊讶的是,似乎没有多少人(即使是在 SO 上)获得参考 :)
    猜你喜欢
    • 1970-01-01
    • 2014-07-26
    • 2010-12-10
    • 2013-08-30
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    相关资源
    最近更新 更多