作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请考虑以下示例:
查看模型
public class FooViewModel
{
public DateTime Date { get; set; }
}
控制者
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index(FooViewModel foo)
{
return View(foo);
}
[HttpPost]
public ActionResult Submit(FooViewModel foo)
{
return RedirectToAction("Index", foo);
}
}
查看
@model MvcApplication1.Models.FooViewModel
<h1>@Model.Date</h1>
@using (Html.BeginForm("Submit", "Home", FormMethod.Post))
{
@Html.TextBoxFor(m => m.Date)
<input type="submit" value"Submit" />
}
路线
routes.MapRoute(
null,
"",
new { controller = "Home", action = "Index" }
);
routes.MapRoute(
null,
"{action}",
new { controller = "Home" },
new { action = "Submit" }
);
问题是在提交表单后,日期编辑器获取的值是切换了日期和月份:
我该怎么做才能让它正常工作?
实际上我尝试格式化编辑器的值:
@Html.TextBoxFor(m => m.Date, new { value = Model.Date.ToString("dd.MM.yyyy") })
EditorFor
帮助程序来使用模板。DataAnnotations
:[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MMM/dd/yyyy}")]
这些都没有改变任何东西。编辑器的值既没有改变,也没有格式化。
最后,我(遵循 the answer by DrJokepu)设法使它仅适用于原始 HTML,即 DateTime
没有辅助方法:
public static MvcHtmlString GetDateEditor(this FooViewModel model)
{
string temp = "<input id=\"Date\" type=\"text\" value=\"{0}\" name=\"Date\"
data-val-required=\"Please, enter a valid date\"
data-val=\"true\" />";
return MvcHtmlString.Create(
string.Format(temp,
model == null
? string.Empty
: model.Date.ToString("dd.MM.yyyy")));
}
如果有一种方法可以使用任何辅助方法实现相同的结果,这对我来说仍然是个 secret ...
最佳答案
请尝试稍作改动的版本:@Html.TextBoxFor(m => m.Date, new { @Value = Model.Date.ToString("dd.MM.yyyy") })
关于c# - TextBoxFor 助手在日期中混合日期和月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15942019/
我是一名优秀的程序员,十分优秀!