asp.net-mvc { 得到; 放; 在 ViewModel 中使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11399375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:05:43  来源:igfitidea点击:

{ get; set; } used in ViewModel

asp.net-mvcrazor

提问by Nate Pet

I am using c# along with a ViewModel that passes the view model from the controller to the view.

我使用 c# 和 ViewModel 将视图模型从控制器传递到视图。

In my view model, the following seems to work as expected as it passes the Description information from the view back to the controller:

在我的视图模型中,以下内容似乎按预期工作,因为它将描述信息从视图传递回控制器:

    public string Description { get; set; } 

But if I have the following, it won't pass back the Description. Description shows null

但是如果我有以下内容,它就不会传回描述。说明显示为空

 public string Description  

Why is the { get; set; }

为什么是 { get; 放; }

so important?

很重要?

回答by Jf Beaulac

I dont know much about asp.net MVC / Razor, but there is an important difference between your 2 code samples.

我不太了解 asp.net MVC/Razor,但是您的 2 个代码示例之间存在重要区别。

public string Description { get; set; }  

Creates a property, once compiled, there is a generated private field in the class, with get/set methods that access the field. A property declared with {get;set;} is the equivalent of:

创建一个属性,一旦编译,在类中有一个生成的私有字段,带有访问该字段的get/set方法。用 {get;set;} 声明的属性相当于:

    private string _description;
    public string Description
    {
        get
        {
            return _description;
        }
        set
        {
            this._description = value;
        }
    }

However the following:

但是以下内容:

public string Description;

Creates a simple public field.

创建一个简单的公共字段。

My guess is that razor uses reflection to get values from the ViewModel, and it probably looks for a property, not a field. So it determines that the property does not exist, hence returning null

我的猜测是 razor 使用反射从 ViewModel 获取值,它可能会寻找一个属性,而不是一个字段。所以它确定该属性不存在,因此返回null

回答by Channs

The below syntax is a C# language feature 'automatic properties'.

以下语法是 C# 语言功能“自动属性”。

public string Description { get; set; }

ASP.NET MVC uses reflectionand data-bindingwhich work only with properties and not variables. Using properties for public access is the way to go.

ASP.NET MVC 使用reflectiondata-binding仅适用于属性而不适用于变量。使用公共访问的属性是要走的路。

Suggest reading thisarticle where the author retracted his 'dislike' for public properties.

建议阅读这篇文章,其中作者收回了他对公共财产的“不喜欢”。

回答by VJAI

The default model binderis the one that binds the request values to properties in models. It binds the values only to public get-set propertiesand not even to public fields.

default model binder是结合该请求值到在模型的属性之一。它仅将值绑定到公共 get-set 属性,甚至不绑定到公共字段。

If you want to bind the values to fields then you have to write your own model binder but public properties or better than public fields so you don't need that.

如果您想将值绑定到字段,那么您必须编写自己的模型绑定器,但公共属性或比公共字段更好,因此您不需要它。

回答by Sangeet Shah

It's a so-called auto property, and is essentially a shorthand for the following (similar code will be generated by the compiler):

这是一个所谓的 auto 属性,本质上是以下内容的简写(类似的代码将由编译器生成):

private string name;
public string Name
{
    get
    {
        return this.name;
    }
    set
    {
        this.name = value;
    }
}