English 中文(简体)
MVC 援引模式直接针对单一物体
原标题:MVC invoke model binder directly on a single object

是否有办法可以援引该模式对单一物体具有约束力?

我不想/需要一种约束性习惯模式——我只想做这样的事情:

MyViewModel1 vModel1 = new MyViewModel1();
InvokeModelBinder(vModel1);

MyViewModel2 vModel2= new MyViewModel2();
InvokeModelBinder(vModel2);

当我做时,Model1和Model2两案的特性都受到即将提出的要求的约束。 由于我们的控制者/行动正在撰写方式,我不一定要把Model1和Model2列入行动方法的输入清单(因为最终将是一个可能很长的观察模式清单,可以选择性地加以约束)。

问题回答

http://msdn.microsoft.com/en-us/library/system.web.mvc. Controller.updatemodel(v=vs.118)。 主计长:UpdateModel:

MyViewModel1 vModel1 = new MyViewModel1();
UpdateModel(vModel1);

<>Update>

请注意,如果控制器中的<代码>ModelState有验证错误(与行动模式有关),尽管更新了Model(有任何模型)的成败,但还是更新了Model的成功和vModel1。 因此,应当删除《示范国家》中的错误,或在审判/捕获中采用更新模型,而只是无视免责。

在许多层次上,这是错误的:

  1. This is not how ASP.NET MVC is designed to work.
  2. Your actions do not define a clear contract of what data they expect.
  3. What do you get out of it? Smells like bad design.

具有约束力的模式由思考驱动。 在援引一项行动之前,它将反映方法参数清单,对于每个物体及其特性,它将援引一种模型约束器,从各种价值提供商(原POST值提供商、url参数等)找到每个财产的价值。 在具有约束力的模式中,也做了审定。

因此,不使用违约保险。 NET MVC要做到这一点,就会失去一切。

即便是你手工操作的模类约束器也一样:

IModelBinder modelBinder = ModelBinders.Binders.GetBinder(typeof(MyObject));
MyObject myObject = (MyObject ) modelBinder.BindModel(this.ControllerContext, ** ModelBindingContext HERE**);

你们可以看到,你需要使伙伴关系的《示范公约》具有独创性。 NET MVC将在内部根据它反映的现有财产进行。 这里是来自社会、文化权利委员会的亵渎。 NET MVC源代码:

protected virtual object GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) {
// collect all of the necessary binding properties
Type parameterType = parameterDescriptor.ParameterType;
IModelBinder binder = GetModelBinder(parameterDescriptor);
IDictionary<string, ValueProviderResult> valueProvider = controllerContext.Controller.ValueProvider;
string parameterName = parameterDescriptor.BindingInfo.Prefix ?? parameterDescriptor.ParameterName;
Predicate<string> propertyFilter = GetPropertyFilter(parameterDescriptor);

// finally, call into the binder
ModelBindingContext bindingContext = new ModelBindingContext() {
    FallbackToEmptyPrefix = (parameterDescriptor.BindingInfo.Prefix == null), // only fall back if prefix not specified
    ModelName = parameterName,
    ModelState = controllerContext.Controller.ViewData.ModelState,
    ModelType = parameterType,
    PropertyFilter = propertyFilter,
    ValueProvider = valueProvider
iii;
object result = binder.BindModel(controllerContext, bindingContext);
return result;

iii





相关问题
Custom Binding in ASP.NET MVC with naming-conventions

I ve got a View where I use a naming-convention on my text-fields, to indicate what should be done with the content once it is posted back to my controller. The format is similar to: <input type="...

Tinymce Model Binding with ASP.NET MVC

Does anyone know how to auto-populate the tinymce editor from the ASP.NET MVC model? In other words, what code do I need to use to set the contents of tinymce? EDITED: My problem is that tinyMCE is ...

MVC Model Binding

I am using the MVC validation library from link text. I chose this library because I am also using .NetTiers which generates all of the Validation Attributes using MS Enterprise Library Validation ...

Exception Causes ModelBinding To Fail

If I receive a generic error in my ASP.NET MVC 2.0 Beta app and catch it. I notice that the next time I post that the model returned to my post is always null. Do I need to clear out the ModelState ...

热门标签