Ale*_*lex 70 string asp.net-mvc null viewmodel razor
当没有值时,我曾经收到空字符串:
[HttpPost]
public ActionResult Add(string text)
{
    // text is "" when there's no value provided by user
}
但现在我正在传递一个模型
[HttpPost]
public ActionResult Add(SomeModel Model)
{
    // model.Text is null when there's no value provided by user
}
所以我必须使用?? ""运算符.
为什么会这样?
小智 149
您可以DisplayFormat在模型类的属性上使用该属性:
[DisplayFormat(ConvertEmptyStringToNull = false)]
默认的模型绑定将为您创建一个新的SomeModel.字符串类型的默认值为null,因为它是引用类型,因此它被设置为null.
这是string.IsNullOrEmpty()方法的用例吗?
| 归档时间: | 
 | 
| 查看次数: | 34778 次 | 
| 最近记录: |