用ASP.NET MVC绑定公共字段以及公共属性?

编程入门 行业动态 更新时间:2024-10-22 02:51:16
本文介绍了用ASP.NET MVC绑定公共字段以及公共属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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绑定公共字段以及公共属性?

本文发布于:2023-11-14 00:09:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585676.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   绑定   属性   ASP   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!