Why do I get null instead of empty string when receiving POST request in from Razor View?

前端 未结 3 1372
生来不讨喜
生来不讨喜 2020-12-02 11:20

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         


        
3条回答
  •  长情又很酷
    2020-12-02 11:51

    You can use the DisplayFormat attribute on the property of your model class:

    [DisplayFormat(ConvertEmptyStringToNull = false)]
    

提交回复
热议问题