Asp.net mvc MVC post back drops日期字段值
以下是我的看法:Asp.net mvc MVC post back drops日期字段值,asp.net-mvc,Asp.net Mvc,以下是我的看法: @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <div class="editor-label"> @Html.LabelFor(model => model.Title) </div> <div class="editor-field">
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ExpireDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ExpireDate)
@Html.ValidationMessageFor(model => model.ExpireDate)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
标题和说明已填充,但无论我在expire_date文本框中输入的日期是什么,它都会显示为:
{1/01/0001 12:00:00 AM}
有什么线索吗?当模型绑定器试图将请求值解析为日期时间字段时,它将使用当前线程区域性。因此,例如,如果在
元素中设置了某些特定区域性,则应确保在文本框中输入的日期字符串格式正确
另一方面,如果web.config中没有全球化元素或将区域性设置为auto
(默认值),则当前区域性将由客户端确定。客户端web浏览器发送包含要使用的值的Accept Language请求标头。在这种情况下,您需要使用与浏览器中配置的日期格式相同的日期格式
如果希望所有日期都采用特定格式,无论使用何种浏览器设置,都可以编写自定义模型绑定器,在对值进行出价时,该绑定器将在视图模型上使用
DisplayFormat
属性。我已经在中演示了这样一个模型绑定器的示例。像这样尝试@Html.LabelFor(model=>model.ExpireDate.ToString(“dd-MMM-yyyy”)
然后我得到了一个例外:“模板只能用于字段访问、属性访问、一维数组索引或单参数自定义索引器表达式。”好的,解决了这个问题。事实证明,我使用的是实体类而不是模型类,mvc框架协调了所有类似命名的属性,但日期除外,日期中有下划线。要注意的一个好的怪癖。
[HttpPost]
public ActionResult Create(bulletin newBulletin)
{
try
{
var db = Util.GetDb();
var toAdd = new bulletin
{
title = newBulletin.title,
description = newBulletin.description,
expire_date = newBulletin.expire_date,
timestamp = DateTime.Now,
user_id = 1
};
db.bulletins.InsertOnSubmit(toAdd);
db.SubmitChanges();
return RedirectToAction("Index");
}
catch(Exception ex)
{
return View();
}
}