asp.net-mvc 带有剃刀视图引擎的 mvc3 中的日期选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9990333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:50:56  来源:igfitidea点击:

Date picker in mvc3 with razor view engine

asp.net-mvcasp.net-mvc-3razor

提问by Sreedhar goud

I am using mvc3 and razor is my view engine how i get date picker with out using scripts in my
view.

我正在使用 mvc3,而 razor 是我的视图引擎,我如何在不使用脚本的情况下获取日期选择器

回答by yusef

You can create a script in the directory scripts of your project.

您可以在项目的目录脚本中创建脚本。

Basic example:

基本示例:

$(document).ready(function () { $("#datepicker").datepicker({ });});

in your view:

在您看来:

@model YourProjectName.Models.User

....

....

<div class="editor-field">
  @Html.TextBoxFor(model => model.Dateadd, new { @Value = DateTime.Now, id = "datepicker" })
  @Html.ValidationMessageFor(model => model.Dateadd)
</div>

回答by Joey Ciechanowicz

I think you are going to have to use a script, check out jqueryui datepicker. Its a nice easy to use library and supports theming

我认为您将不得不使用脚本,请查看jqueryui datepicker。它是一个很好的易于使用的库并支持主题

回答by Matija Grcic

I answered here, check it out: http://forums.asp.net/post/4647234.aspx

我在这里回答,看看:http: //forums.asp.net/post/4647234.aspx

Basically you're using a template with a script in one location and calling it with EditorFor.

基本上,您在一个位置使用带有脚本的模板,并使用 EditorFor 调用它。

回答by Alexander

To advisers here: it's a bad practice to use scripts inside partial views (templates).

对于这里的顾问:在部分视图(模板)中使用脚本是一种不好的做法。

In my case it does not work at all. Because accessing jquery happens before it's included as js file. Plus you cannot predict where exactly you would put this datepicker control. Also, you will have this "ready" block for every editor on the page.

就我而言,它根本不起作用。因为访问 jquery 发生在它被包含为 js 文件之前。另外,您无法预测将此日期选择器控件放在哪里。此外,您将为页面上的每个编辑器设置这个“就绪”块。

RenderSection would bring some order to all this, but it does not work for partialviews and templates.

RenderSection 会为所有这些带来一些秩序,但它不适用于局部视图和模板。

So just move javascript code from a template (partialview) to a view.

因此,只需将 javascript 代码从模板(部分视图)移动到视图。

回答by Shahnawaz

@model Nullable<System.DateTime>
@if ( Model.HasValue ) {
@Html.TextBox( "" , String.Format( "{0:yyyy-MM-dd HH:mm}" , Model.Value ) , new  { 
@class = "textbox" , @style = "width:400px;" } )
}
else {
@Html.TextBox( "" , String.Format( "{0:yyyy-MM-dd HH:mm}" , DateTime.Now ) , new {   
@class = "textbox" , @style = "width:400px;" } )
}

@{
string name = ViewData.TemplateInfo.HtmlFieldPrefix;
string id = name.Replace( ".", "_" );
}
<script type="text/javascript">
$(document).ready(function () {
$("#@id").datepicker
({
dateFormat: 'dd/mm/yy',
showStatus: true,
showWeeks: true,
highlightWeek: true,
numberOfMonths: 1,
showAnim: "scale",
showOptions: {
origin: ["top", "left"]
}
});
});
</script>

回答by Prof Von Lemongargle

If you use an Editor Template for the DateTime type, you can use an HTML5 date picker (i.e. <input type="date" />). Essentially you put a view called DateTime.cshtml in a folder called Views/Shared/EditorTemplates, then you can style the editor however you like. One example is in an answer here.

如果您使用 DateTime 类型的编辑器模板,则可以使用 HTML5 日期选择器(即<input type="date" />)。本质上,您将一个名为 DateTime.cshtml 的视图放在名为 Views/Shared/EditorTemplates 的文件夹中,然后您可以随意设置编辑器的样式。一个例子是这里的答案。