用ASP.NET MVC绑定公共字段以及公共属性? [英] Binding public fields with ASP.NET MVC as well as public properties?

查看:170
本文介绍了用ASP.NET MVC绑定公共字段以及公共属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很明显,ASP.NET MVC的绑定功能负责在将模型的公共属性传递给控制器​​时对其进行绑定,因此例如在以下示例中,SurnameEmail将使用其提交的值进行绑定:

Obviously, ASP.NET MVC's binding functionality takes care of binding a model's public properties when passing it to a controller, so for instance in the following example, Surname and Email will be bound with their submitted values:

public ActionResult Create(UserModel mdlNewUser) {
    // ...
}

// ...

public class UserModel {
    public string Firstname;
    public string Surname { get; set; }
    public string Email { get; set; }
}

但是,在上面的示例中,似乎并没有自动绑定诸如Firstname之类的公共字段;这些将保持不变.有什么方法可以使公共字段(以及其他类型的类成员)自动绑定,还是只能绑定公共属性?

However, it doesn't seem to auto-bind public fields such as Firstname in the above example; these will be left untouched. Is there any way to get public fields (and for that matter, any other type of class member) to be automatically bound, or will it only ever bind public properties?

本文似乎暗示它只是公共财产,因为它一直都只提到它们,但是似乎并没有明确地说只有公共的 properties 会被绑定.

This article seems to imply that it's only public properties because it only ever refers to them all the way through, but it doesn't explicitly seem to say that only public properties will be bound.

推荐答案

是的,只有公共属性会被绑定.

That's correct, only public properties will be bound.

字段不能用于绑定.

这篇关于用ASP.NET MVC绑定公共字段以及公共属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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