Asp.net mvc Mvc 5在编辑日期值时,如果我用
我有一个日期字段,我想用当前值填充它,让我同时选择日期,但这不会发生。 当我对编辑行执行model.CourseDate.Date时,如下所示:Asp.net mvc Mvc 5在编辑日期值时,如果我用,asp.net-mvc,model-view-controller,asp.net-mvc-5,Asp.net Mvc,Model View Controller,Asp.net Mvc 5,我有一个日期字段,我想用当前值填充它,让我同时选择日期,但这不会发生。 当我对编辑行执行model.CourseDate.Date时,如下所示: <div class="form-group"> @Html.LabelFor(model => model.CourseDate, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-1
<div class="form-group">
@Html.LabelFor(model => model.CourseDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CourseDate.Date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CourseDate, "", new { @class = "text-danger" })
</div>
</div>
@LabelFor(model=>model.CourseDate,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.CourseDate.Date,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.CourseDate,“,new{@class=“text danger”})
这种情况会发生
如果我这样做的话
<div class="form-group">
@Html.LabelFor(model => model.CourseDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CourseDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CourseDate, "", new { @class = "text-danger" })
</div>
</div>
@LabelFor(model=>model.CourseDate,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.CourseDate,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.CourseDate,“,new{@class=“text danger”})
发生这种情况:
我想填写该字段,同时可以从下拉列表中选择日期。这里有两种不同的表示法: 1) 在第一个
EditorFor
上,它作为CourseDate
中的DateTime
实例的一部分绑定,因此它呈现为带有
的普通文本框。所以,
@Html.EditorFor(model => model.CourseDate.Date, new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(model => model.CourseDate, new { htmlAttributes = new { @class = "form-control" } })
呈现为:
<input id="CourseDate_Date" name="CourseDate.Date" type="datetime" value="...">
<input id="CourseDate" name="CourseDate" type="date" value="...">
呈现为:
<input id="CourseDate_Date" name="CourseDate.Date" type="datetime" value="...">
<input id="CourseDate" name="CourseDate" type="date" value="...">
请注意,
元素需要格式化为yyyy-MM-dd
格式的值来根据显示其值,因此字符串格式{0:yyyy-MM-dd}
用于匹配该格式
包含一个示例,其中用户应能够通过使用
元素中的日期选择器手动输入日期组件
参考:
我相信
CourseDate
是一个DateTime
属性,并且模型。CourseDate.Date
可能是指CourseDate
属性的Date
属性。请注意,EditorFor
可能会自动为DateTime
属性呈现日期输入,但不会为DateTime
实例中的date
属性呈现日期输入。@TetsuyaYamamoto您可能是对的,我希望两者都能以某种方式工作。我无法同时显示当前日期和打开框。您看到的第二个输入是带有微调器按钮的
,第一个输入是标准的
。如果您希望两个属性使用相同的输入类型,请使用饰有type=“date”
的TextBoxFor
帮助程序。谢谢你,伙计,我不知道TextBoxFor的重载方法,所以我无法在应用程序中尝试,但在你给出第二个参数后,现在它工作起来很有魅力。