为什么我得到空字符串空而不是从剃刀查看接收POST请求时? [英] Why do I get null instead of empty string when receiving POST request in from Razor View?

查看:190
本文介绍了为什么我得到空字符串空而不是从剃刀查看接收POST请求时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前当有接收空字符串没有值:

I used to receive empty string when there was no value:

[HttpPost]
public ActionResult Add(string text)
{
    // text is "" when there's no value provided by user
}

但现在我路过一个模型

But now I'm passing a model

[HttpPost]
public ActionResult Add(SomeModel Model)
{
    // model.Text is null when there's no value provided by user
}

所以,我必须使用 ?? 运营商。

为什么会出现这种情况?

Why is this happening?

推荐答案

您可以使用下面的属性,你的模型类的属性。

You can use the following attribute on the property of your model class.

            [DisplayFormat(ConvertEmptyStringToNull = false)]

这篇关于为什么我得到空字符串空而不是从剃刀查看接收POST请求时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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