开发者

asp.net mvc bind a specific model

I have a model with a coll开发者_Go百科ection

public class Model
{
     IEnumerable<ModelPart> Parts {get;set;}
}

public class Parts 
{
     public string Prop1 {get;set;}
     public string Prop2 {get;set;}
}

But the comming in url is quite nasty.

It has this form

dhxGridObj_d3BIc6JfDidc_1_0=&dhxGridObj_d3BIc6JfDidc_1_1=sssss&dhxGridObj_d3BIc6JfDidc_1_2=ssssss& dhxGridObj_d3BIc6JfDidc_2_0=&dhxGridObj_d3BIc6JfDidc_2_1=aaaa&dhxGridObj_d3BIc6JfDidc_2_2=aaaaa

It has three parts separated with underscore

dhxGridObj_d3BIc6JfDidc_2_1

  1. Some id dhxGridObj_d3BIc6JfDidc
  2. Row id 2
  3. Cell id 1

I'm wondering what would be the best way bind this to my model.

I was thinking of renaming this dhxGridObj_d3BIc6JfDidc_2_1 to model[2].Prop1 Where would be the best place to do this ?


Oh yeah, that's a hell of an ugly request string. A custom model binder is the way to parse this beast until you find a real solution to this problem which of course is fixing the system sending this crap to conform to the default model binder syntax.

And here's an example of some scratch code that might put you on the right track:

public class MyModelBinder : DefaultModelBinder
{
    private const string Prefix = "dhxGridObj_d3BIc6JfDidc";

    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var model = base.BindModel(controllerContext, bindingContext) as Model;
        var request = controllerContext.HttpContext.Request;
        model.Parts = request
            .Params
            .Keys
            .OfType<string>()
            .Select(key => Regex.Match(key, Prefix + "_([0-9]+)_([0-9]+)"))
            .Where(x => x.Success)
            .Select(x => new
            {
                Row = x.Groups[1].Value,
                Col = x.Groups[2].Value
            })
            .GroupBy(x => x.Row)
            .Select(x => new Parts
            {
                Prop1 = request[string.Format("{0}_{1}_{2}", Prefix, x.Key, x.ElementAt(0).Col)],
                Prop2 = request[string.Format("{0}_{1}_{2}", Prefix, x.Key, x.ElementAt(1).Col)],
            });
        return model;
    }
}

which will be registered in Application_Start:

ModelBinders.Binders.Add(typeof(Model), new MyModelBinder());


You should not use query string parameters in url of your MVC application. the best for this is Url routes. you can find a good article for routes in MVC here :

http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜