C# 如何在自定义模型绑定器中验证我的模型?

C# 如何在自定义模型绑定器中验证我的模型?,c#,validation,asp.net-mvc-4,C#,Validation,Asp.net Mvc 4,我问了一个关于逗号分隔数值的问题 给出了一些回复,我尝试实现我自己的模型绑定器,如下所示: namespace mvcapapplication1.Core { 公共类PropertyModelBinder:DefaultModelBinder { 公共重写对象BindModel(ControllerContext ControllerContext,ModelBindingContext bindingContext) { object objectModel=新对象(); if(bindin

我问了一个关于逗号分隔数值的问题

给出了一些回复,我尝试实现我自己的模型绑定器,如下所示:

namespace mvcapapplication1.Core
{
公共类PropertyModelBinder:DefaultModelBinder
{
公共重写对象BindModel(ControllerContext ControllerContext,ModelBindingContext bindingContext)
{
object objectModel=新对象();
if(bindingContext.ModelType==typeof(PropertyModel))
{
HttpRequestBase请求=controllerContext.HttpContext.request;
string price=request.Form.Get(“price”).Replace(“,”,string.Empty);
ModelBindingContext newBindingContext=新ModelBindingContext()
{
ModelMetadata=ModelMetadataProviders.Current.GetMetadataForType(
()=>新属性模型()
{
价格=转换为32(价格)
},
类型(PropertyModel)
),
ModelState=bindingContext.ModelState,
ValueProvider=bindingContext.ValueProvider
};
//在此新绑定上下文中调用默认模型绑定器
返回base.BindModel(controllerContext、newBindingContext);
}
其他的
{
返回base.BindModel(controllerContext、bindingContext);
}
}
//受保护的重写对象CreateModel(ControllerContext ControllerContext,ModelBindingContext bindingContext,Type modelType)
//{
////返回base.CreateModel(controllerContext、bindingContext、modelType);
//PropertyModel模型=新的PropertyModel();
//if(modelType==typeof(PropertyModel))
//    {
//model=(PropertyModel)base.CreateModel(controllerContext、bindingContext、modelType);
//HttpRequestBase请求=controllerContext.HttpContext.request;
//string price=request.Form.Get(“price”).Replace(“,”,string.Empty);
//model.Price=Convert.ToInt32(价格);
//    }
//收益模型;
//}
}
}
并将我的控制器类更新为:

namespace mvcapapplication1.Controllers
{
公共类属性控制器:控制器
{
公共行动结果编辑()
{
PropertyModel模型=新的PropertyModel
{
AgentName=“John Doe”,
BuildingStyle=“Colonial”,
建筑年份=1978年,
价格=650000,
Id=1
};
返回视图(模型);
}
[HttpPost]
公共操作结果编辑([ModelBinder(typeof(PropertyModelBinder))]PropertyModel模型)
{
if(ModelState.IsValid)
{
//保存属性信息。
}
返回视图(模型);
}
关于()的公共行动结果
{
ViewBag.Message=“您的应用程序描述页。”;
返回视图();
}
公共行动结果联系人()
{
ViewBag.Message=“您的联系人页面。”;
返回视图();
}
}
}
现在,如果我用逗号输入价格,我的自定义模型绑定器将删除逗号,这就是我想要的,但验证仍然失败。所以,问题是:如何在我的自定义模型绑定器中进行自定义验证,从而避免使用逗号捕获价格值?换句话说,我怀疑我需要在自定义模型活页夹中做更多的工作,但不知道如何做和做什么。谢谢

更新:

因此,我尝试了@mare的解决方案,并更新了我的模型活页夹,如下所示:

namespace mvcapapplication1.Core
{
公共类PropertyModelBinder:DefaultModelBinder
{
公共重写对象BindModel(ControllerContext ControllerContext,ModelBindingContext bindingContext)
{
object objectModel=新对象();
if(bindingContext.ModelType==typeof(PropertyModel))
{
HttpRequestBase请求=controllerContext.HttpContext.request;
string price=request.Form.Get(“price”).Replace(“,”,string.Empty);
ModelBindingContext newBindingContext=新ModelBindingContext()
{
ModelMetadata=ModelMetadataProviders.Current.GetMetadataForType(
()=>新属性模型()
{
价格=转换为32(价格)
},
类型(PropertyModel)
),
ModelState=bindingContext.ModelState,
ValueProvider=bindingContext.ValueProvider
};
//在此新绑定上下文中调用默认模型绑定器
对象o=base.BindModel(controllerContext,newBindingContext);
newBindingContext.ModelState.Remove(“价格”);
添加(“Price”,newmodelstate());
newBindingContext.ModelState.SetModelValue(“Price”,新的ValueProviderResult(Price,Price,null));
返回o;
}
其他的
{
返回base.BindModel(controllerContext、bindingContext);
}
}
//受保护的重写对象CreateModel(ControllerContext ControllerContext,ModelBindingContext bindingContext,Type modelType)
//{
////返回base.CreateModel(controllerContext、bindingContext、modelType);
//PropertyModel模型=新的PropertyModel();
//if(modelType==typeof(PropertyModel))
//    {
//模型=
object o = base.BindModel(controllerContext, newBindingContext);
newBindingContext.ModelState.Remove("Price");
newBindingContext.ModelState.Add("Price", new ModelState());
newBindingContext.ModelState.SetModelValue("Price", new ValueProviderResult(price, price, null));
return o;
newBindingContext.ModelState.Remove("Price");
newBindingContext.ModelState.Add("Price", new ModelState());
newBindingContext.ModelState.SetModelValue("Price", new ValueProviderResult(price, price, null));
object o = base.BindModel(controllerContext, newBindingContext);
return o;
    [HttpPost]
    public ActionResult Edit([ModelBinder(typeof(PropertyModelBinder))]PropertyModel model)
    {
        ModelState.Clear();
        TryValidateModel(model);
        if (ModelState.IsValid)
        {
            //Save property info.              
        }

        return View(model);
    }
public class PropertyModelBinder : DefaultModelBinder
{     

    protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
    {
        if(propertyDescriptor.ComponentType == typeof(PropertyModel))
        {
            if (propertyDescriptor.Name == "Price")
            {
                var obj=   bindingContext.ValueProvider.GetValue("Price");
                return Convert.ToInt32(obj.AttemptedValue.ToString().Replace(",", ""));
            }
        }
        return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
    }       
}
//In Global.asax
ModelBinders.Binders.Add(typeof(Property), new PropertyRegistrationModelBinder());
ModelBinders.Binders.Add(typeof(Agent), new PropertyRegistrationModelBinder());

//Updated ModelBinder look like this.

 public class PropertyRegistrationModelBinder : DefaultModelBinder
{
    protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
    {
        if (propertyDescriptor.ComponentType == typeof(Property) || propertyDescriptor.ComponentType == typeof(Agent))
        {
            if(propertyDescriptor.Name == "Price" || propertyDescriptor.Name == "AnnualSales")
            {                    
                var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue.Replace(",", string.Empty);
                return string.IsNullOrEmpty(value) ? 0 : Convert.ToInt32(value);
            }
        }            
        return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
    }
}