twitter-bootstrap MVC:从视图到模型绑定日期时间值(引导日期时间选择器)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17299569/
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
MVC : Binding datetime value (bootstrap datetime picker) from view to model
提问by Cybercop
I want to bind the date time value that I get from view to my model, so that I could use to save it in database.
Note: Currently I'm using bootstrap datetime picker to get date time value.
Let us say I have a ViewModel called foo
我想将从视图中获得的日期时间值绑定到我的模型,以便我可以将其保存在数据库中。
注意:目前我正在使用引导程序日期时间选择器来获取日期时间值。
假设我有一个名为 foo 的 ViewModel
public class fooVM
{
public string Name {get; set; }
[DataType(DataType.DateTime)]
public DateTime DateEntered {get; set}
}
based on this I have a view
基于此我有一个观点
@model User.Model.fooVM
@{
ViewBag.Title = "Create";
Layout = "~/Views/shared/_BootstrapLayout.basic.cshtml";
}
@section Datetime
{
<link href="@Url.Content("~/Content/bootstrap.css")" rel="stylesheet" type="text/css"/>
<link href="@Url.Content("~/Content/bootstrap-datetimepicker.min.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-2.0.2.min.js")"></script>
<script type="text/javascript" src="~/Scripts/bootstrap.min.js"></script>
<script type="text/javascript" src="~/Scripts/bootstrap-datetimepicker.min.js"></script>
}
@using (Html.BeginForm("Register", "Account", FormMethod.Post))
{
<div class="input-block-level">@Html.TextBoxFor(model => model.Name</div>
<div id="datetimepicker2" class="input-append date">
<input data-format="dd/MM/yyyy HH:mm:ss PP" type="text"/>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('#datetimepicker2').datetimepicker({
format: 'dd/MM/yyyy hh:mm:ss',
language: 'en',
pick12HourFormat: true
});
});
</script>
</div>
}
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</fieldset>
Now this would give me the date time picker in view but the value would not bind to my DateEnteredvalue in model.
现在这将为我提供日期时间选择器,但该值不会绑定到我DateEntered在模型中的值。
If I wanted to do get the value from view for date time and save it may be in database, how would i do it? so that if i executed following command in my controller it would work.
如果我想从视图中获取日期时间的值并将其保存在数据库中,我该怎么做?这样如果我在控制器中执行以下命令,它就会起作用。
[HttpPost]
public ActionResult Create(fooVM user)
{
if (ModelState.IsValid) {
_db.Users.Add(user);
_db.SaveChanges();
}
PS: This could be possible value in database
Name : ABC
DateEntered : 6/14/2013 9:34:23 AM
PS:这可能是数据库
名称中的可能值:ABC
DateEntered:6/14/2013 9:34:23 AM
回答by cadrell0
If you set the Name attribute on your input, I believe MVC will match that to the property on your model with the same name.
如果您在输入上设置 Name 属性,我相信 MVC 会将其与模型上具有相同名称的属性相匹配。
Alternatively, you can use HtmlTextBoxForhelper and use the htmlAttributesparameter to set data-format. Something like @Html.TextBoxFor(model => model.DateEntered, new { data_format = 'dd/MM/yyyy HH:mm:ss PP'});
或者,您可以使用HtmlTextBoxForhelper 并使用htmlAttributes参数来设置data-format. 就像是@Html.TextBoxFor(model => model.DateEntered, new { data_format = 'dd/MM/yyyy HH:mm:ss PP'});
Here is the MSDNfor this overload.
这是此重载的MSDN。
回答by Lee Bailey
You need to give the input tag a name that corresponds to a property in your model, like this:
您需要为输入标签指定一个与模型中的属性相对应的名称,如下所示:
<input name="DateEntered" data-format="dd/MM/yyyy HH:mm:ss PP" type="text"/>
回答by Andy Brudtkuhl
All you should need to do is give the appropriate "name" attribute to the bootstrap date select input that maps to the desired property in your view model.
您需要做的就是为映射到视图模型中所需属性的引导日期选择输入提供适当的“名称”属性。
<input data-format="dd/MM/yyyy HH:mm:ss PP" type="text" name="DateEntered" />

