[英]Model Binding string fields in MVC
好的,我在MVC上遇到問題,我將控制器/視圖連接到多個模型,這些模型包含字符串的受保護內部集。 創建這些對象時,我需要能夠設置字符串。 話雖如此,我在理解ModelBinding來完成此任務時遇到了問題。 我已經為ModelBinder附加了一個非常基本的設置,但是不知道從哪里可以找到:
/// <summary>
/// Handles binding for the string variables
/// </summary>
public class ActionResultModelBinder : DefaultModelBinder, IModelBinder, ITypedModelBinder
{
#region Properties
/// <summary>
/// Gets the type that this model binder's associated with
/// </summary>
/// <value>
/// The type that this model binder's associated with.
/// </value>
public Type AssociatedType
{
get
{
return typeof(string);
}
}
#endregion Properties
#region Methods
/// <summary>
/// Binds the model by using the specified controller context and binding context.
/// </summary>
/// <param name="controllerContext">The context within which the controller operates. The context information includes the controller, HTTP content, request context, and route data.</param>
/// <param name="bindingContext">The context within which the model is bound. The context includes information such as the model object, model name, model type, property filter, and value provider.</param>
/// <returns>
/// The bound object.
/// </returns>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="bindingContext "/>parameter is null.</exception>
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var boundValue = base.BindModel(controllerContext, bindingContext);
return bindingContext.ModelType == typeof(string);
}
/// <summary>
/// Sets the specified property by using the specified controller context, binding context, and property value.
/// </summary>
/// <param name="controllerContext">The context within which the controller operates. The context information includes the controller, HTTP content, request context, and route data.</param>
/// <param name="bindingContext">The context within which the model is bound. The context includes information such as the model object, model name, model type, property filter, and value provider.</param>
/// <param name="propertyDescriptor">Describes a property to be set. The descriptor provides information such as the component type, property type, and property value. It also provides methods to get or set the property value.</param>
/// <param name="value">The value to set for the property.</param>
protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value)
{
if (propertyDescriptor.PropertyType == typeof(string))
{
var stringVal = value as string;
}
base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
}
#endregion Methods
}
好,讓我解釋一下。
您想綁定一個模型,因此首先需要將模型返回
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
這是用於綁定名為FacebookGroupViewModel的模型的自定義綁定程序的示例:
public class FacebookGroupViewModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var model = new FacebookGroupViewModel();
if(controllerContext.HttpContext.Request.Form.AllKeys.Contains("Friends"))
{
var friends = controllerContext.HttpContext.Request.Form["Friends"].Split(',');
foreach (var friend in friends)
{
model.FacebookFriendIds.Add(friend);
}
}
return model;
}
}
在這里您可以看到我從表單中獲取了一個值:
controllerContext.HttpContext.Request.Form["Friends"]
但是您可以從QueryString或其他任何想要的值中獲取值,因為這里有HttpContext。 調試並查看所有必須了解更多信息的屬性。
最后,您需要設置此綁定器,並通過這種方式將其綁定到global.asax中的模型。
ModelBinders.Binders.Add(typeof(FacebookGroupViewModel),new FacebookGroupViewModelBinder());
在應用程序中啟動方法。
然后,只需想象一下,我的控制器就是這樣。
[HttpPost]
public ActionResult PostFriend(FacebookGroupViewModel model)
{
//here your model is binded by your custom model ready to use.
}
工作完成,如果您還有其他疑問,請告訴我。
您實際上不需要的更多信息 :
protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value)
允許您使用默認的綁定器行為並覆蓋某些屬性,就像您僅使用字符串一樣,但是為此,您需要使用原始的bindModel方法。(例如base.BindModel)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.