过去,当没有值时,我会收到空字符串:
[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
}
所以我必须使用?? “”
运算符。
为什么会发生这种情况?
最佳答案
您可以使用DisplayFormat
模型类属性上的属性:
[DisplayFormat(ConvertEmptyStringToNull = false)]
关于asp.net-mvc - 当从 Razor View 接收 POST 请求时,为什么我得到的是 null 而不是空字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3641723/