当从Razor视图接收POST请求时,为什么会得到null而不是空字符串?

时间:2021-07-17 11:50:37

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
}

So I have to use the ?? "" operator.

所以我必须用?? ?”“运营商。

Why is this happening?

为什么会这样?

3 个解决方案

#1


128  

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

您可以在模型类的属性上使用DisplayFormat属性:

[DisplayFormat(ConvertEmptyStringToNull = false)]

#2


8  

The default model binding will create a new SomeModel for you. The default value for the string type is null since it's a reference type, so it's being set to null.

默认的模型绑定将为您创建一个新的SomeModel。字符串类型的默认值为null,因为它是一个引用类型,所以它被设置为null。

Is this a use case for the string.IsNullOrEmpty() method?

这是string.IsNullOrEmpty()方法的用例吗?

#3


1  

I am trying this in Create and Edit (my object is called 'entity'):-

我正在创建和编辑(我的对象叫做“实体”):-

        if (ModelState.IsValid)
        {
            RemoveStringNull(entity);
            db.Entity.Add(entity);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(entity);
    }

Which calls this:-

要求:-

    private void RemoveStringNull(object entity)
    {
        Type type = entity.GetType();
        FieldInfo[] fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.GetField | BindingFlags.Public | BindingFlags.NonPublic);
        for (int j = 0; j < fieldInfos.Length; j++)
        {
            FieldInfo propertyInfo = fieldInfos[j];
            if (propertyInfo.FieldType.Name == "String" )
            {
                object obj = propertyInfo.GetValue(entity);
                if(obj==null)
                    propertyInfo.SetValue(entity, "");
            }
        }
    }

It will be useful if you use Database First and your Model attributes get wiped out each time, or other solutions fail.

如果您首先使用数据库,并且每次都删除模型属性,或者其他解决方案失败,那么这将非常有用。

#1


128  

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

您可以在模型类的属性上使用DisplayFormat属性:

[DisplayFormat(ConvertEmptyStringToNull = false)]

#2


8  

The default model binding will create a new SomeModel for you. The default value for the string type is null since it's a reference type, so it's being set to null.

默认的模型绑定将为您创建一个新的SomeModel。字符串类型的默认值为null,因为它是一个引用类型,所以它被设置为null。

Is this a use case for the string.IsNullOrEmpty() method?

这是string.IsNullOrEmpty()方法的用例吗?

#3


1  

I am trying this in Create and Edit (my object is called 'entity'):-

我正在创建和编辑(我的对象叫做“实体”):-

        if (ModelState.IsValid)
        {
            RemoveStringNull(entity);
            db.Entity.Add(entity);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(entity);
    }

Which calls this:-

要求:-

    private void RemoveStringNull(object entity)
    {
        Type type = entity.GetType();
        FieldInfo[] fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.GetField | BindingFlags.Public | BindingFlags.NonPublic);
        for (int j = 0; j < fieldInfos.Length; j++)
        {
            FieldInfo propertyInfo = fieldInfos[j];
            if (propertyInfo.FieldType.Name == "String" )
            {
                object obj = propertyInfo.GetValue(entity);
                if(obj==null)
                    propertyInfo.SetValue(entity, "");
            }
        }
    }

It will be useful if you use Database First and your Model attributes get wiped out each time, or other solutions fail.

如果您首先使用数据库,并且每次都删除模型属性,或者其他解决方案失败,那么这将非常有用。