MVC3的HtmlHelper默认 [英] MVC3 HTMLHelper defaults

查看:160
本文介绍了MVC3的HtmlHelper默认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML帮助,我想设置一个默认值。

I have an html helper that I'd like to set a default on.

@Html.EditorFor(model => model.DateFrom)

什么是设置助手的默认值,如果model.DateFrom是空的语法?

What's the syntax to set the default value of the helper if the model.DateFrom is null?

推荐答案

我不认为使用EditorFor你可以设置一个默认值。考虑你的模型设置它的访问?

I dont think that using EditorFor you can set a default value. Consider setting it in the accessors on your model?

要做到这一点对其他类型(TextBoxFor等),你可以设置一个值,而不是默认值。所以,你需要做的:

To do it on other types ( TextBoxFor etc ) You can set a value but not a default value. So you would need to do:

@if(Model.something == null)
{
   @Html.TextBoxFor(m => m.ID, new { @Value = "Value!"})
} else {
   @Html.TextBoxFor(m => m.ID)
}

由于我会建议:

private DateTime? _date;
public DateTime? date {
get {
   if(_date == null)
      _date = DateTime.Now;
   return _date;
}
set {
   _date = value;
}
}

使用的东西,如jQuery的日期选择器让你有一个默认值,如果问题是,你只是没有张贴回来,如果没有被选中。

Using things such as jquery date picker allow you to have a default value if the problem is that you are just posting nothing back if it has not been selected.

这篇关于MVC3的HtmlHelper默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆