asp.net-mvc - 当从 Razor View 接收 POST 请求时,为什么我得到的是 null 而不是空字符串?

标签 asp.net-mvc string razor null viewmodel

过去,当没有值时,我会收到空​​字符串:

[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/

相关文章:

时间:2019-03-07 标签:c#奇怪DictionaryContainsKey或StringComaprer

c# - 将表格置于图像之上

c# - 如何使用 Dapper 从数据库中检索单个值

html - 无法将控件添加到我的 ASP 页面中的现有 DIV 元素

javascript - 从 javascript 和 jquery 中的字符串中删除第一个匹配项

asp.net - 在 ASP.NET MVC View 中渲染 HTML 文件?

c# - 发生 Razor 错误时,未创建位于的 View

javascript - 在 asp.net mvc 中锁定用户之间的 web 对象

c# - 将表单文件转换为内存流时出错

c# - String.Equals 的奇怪行为?