使用编辑器模板(datetime.cshtml)和html.editorfor问题 [英] Using editor template (datetime.cshtml) with html.editorfor problem

查看:355
本文介绍了使用编辑器模板(datetime.cshtml)和html.editorfor问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功为@HtmlEditorFor()方法创建了一个编辑器模板,代码如下所示:

I've successfully created a editor template for the @HtmlEditorFor() method and the code looks like this:

<div class="form-group">
    @Html.LabelFor(model => model.Birthdate, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Birthdate, "DateTime", new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Birthdate, "", new { @class = "text-danger" })
    </div>
</div>



部分视图DateTime如下所示:


And the partial view DateTime looks like this:

@model DateTime?
@if (Model.HasValue)
{
    @Model.Value.ToString("MM/dd/yyyy")
}



...这位于Shared / EditorTemplates子目录中。



模板实际上是格式化但是控件不再是可编辑的字段......它只是显示为简单的HTML格式。



关于为什么不再显示和编辑我的Birthdate属性的区域的任何想法?



我尝试了什么:



我所尝试的完全如上所述...


... and this is located in the Shared/EditorTemplates subdirectory.

The template actually does the formatting BUT the control is no longer an editable field ... it is simply displayed as if it was simple HTML.

Any ideas as to why this is no longer displayed as and editing region for my Birthdate property?

What I have tried:

What I have tried is exactly as stated above ...

推荐答案

因为这正是你告诉它要做的。 :)



您所拥有的内容可以作为显示模板使用。对于编辑器模板,您需要渲染一个可编辑的控件。



这是一个简单的例子:

Because that's exactly what you've told it to do. :)

What you've got there would work fine as a display template. For an editor template, you'll need to render an editable control.

Here's a simplistic example:
@model DateTime?
@{
    var htmlAttributes = new { type = "date", @class = "form-control", placeholder = ViewData.ModelMetadata.Watermark };
}
@Html.TextBox("", Model?.ToString("yyyy-MM-dd"), htmlAttributes)



注意: HTML 日期输入 [ ^ ]使用明确的格式yyyy-MM-dd两者都是属性以及将值发回服务器时。大多数浏览器会自动调整此选项以匹配用户的显示设置。


NB: The HTML date input[^] uses the unambiguous format "yyyy-MM-dd" both for the value attribute and when posting the value back to the server. Most browsers will automatically adjust this to match the user's display settings.


这篇关于使用编辑器模板(datetime.cshtml)和html.editorfor问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆