使用EditFor时,取出非可空性默认值[asp.net MVC 3] [英] Remove default value for non nullable properties when using EditFor [asp.net mvc 3]

查看:160
本文介绍了使用EditFor时,取出非可空性默认值[asp.net MVC 3]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能删除默认情况下使用EditFor助手时,加入非可空属性的文本框的默认值?我不希望这样的行为

How can I remove the default value that is added by default to the textboxes of non nullable properties when using the EditFor helper? I don't want that behavior

修改

对不起,我没有提供足够的信息。

Sorry I didn't give enough information.

例如,如果您使用Html.EditorFor与DateTime的是它会自动文本框的值设置为1/1/0001属性。如果使用日期时间?(可为空),它不会,它只是离开文本框为空。

For example if you use Html.EditorFor with a property that is DateTime it will set the textbox value to 1/1/0001 automatically. If you use "DateTime?"(nullable), it won't, it just leaves the textbox empty.

推荐答案

我不得不做这样的事情对我自己的要求。我用这样的:

I had to do something like this for my own needs. I used this:

@model DateTime?

@Html.TextBox("", (Model.Value != default(DateTime) ? Model.Value.ToShortDateString() : string.Empty))

和它很好的工作pretty我DateTime值。一个没有默认值的是空白的,而且有一些其他的DateTime值的那些表现出物体的ShortDateString重新presentation。

and it worked pretty nicely for my DateTime values. Ones that didn't have the default value are blank and the ones that have some other DateTime value show the ShortDateString representation of the object.

这篇关于使用EditFor时,取出非可空性默认值[asp.net MVC 3]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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