开发者

Asp.net mvc - change keys before model binding

开发者 https://www.devze.com 2023-02-23 06:39 出处:网络
I have to change the keys (controllerContext.HttpContext.Request.Form) before the binding will happ开发者_开发问答en.

I have to change the keys (controllerContext.HttpContext.Request.Form) before the binding will happ开发者_开发问答en.

Is this even possible ? I overrided the BindModel method of DefaultModelBinder and tried controllerContext.HttpContext.Request.Form.Add() but it's readonly.

I cannot change the form keys in the submited html.

For example

key: xxx_xxx_rownumber_cell

new key: Model[1].Prop1

There are also some dataanotations on the model, so validation should not be omited.


You've probably found another solution for this some time ago, but if you still want to do it, you should be able to change the form with something like this:

var collection = controllerContext.HttpContext.Request.Form;
PropertyInfo readOnly = collection.GetType().GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
readOnly.SetValue(collection, false, null);
// Your changes here.
readOnly.SetValue(collection, true, null);


Why not just have your action method take the FormCollection as the parameter, and manually bind however you need it to bind?

Also, you could also create a custom model binder for a specific type to utilize this conversion too I believe.

HTH.

0

精彩评论

暂无评论...
验证码 换一张
取 消