开发者

MVC Razor How to get the model in the Controller on HttpPost when the model is dynamic

I'm working a feature in the application where model will be dynamic in the sense that any settings data could be displayed and the view will get the model based on what tab they clicked on. I use Hidden field to store what the settings name was because they are same as model name. for ex., if tab1-> Settings1 then Model is Settings1[already exists in the Model].So I used @ model dynamic in View and used @Html.EditotForModel() to draw the required UI based off the model. My problem is when I do HttpPost on Edit currently I'm using FormCollection to read the data on that page when I declare the model name in the param it will get it for me but I don't know which model is coming back other than by the Hidden variable and I need it because the Model validation is broken because of this issue. Any help or feedback is appreciated? I c开发者_高级运维an give more details if required? Has anybody crossed this issue before??


Dynamics can be a good thing and a bad thing. Using them on models that have a common interface in a controlled manor is best.

There are different options that you can look at: 1) Have you tried making the action method accept a dynamic type? That might be the easiest way.

You might have to set up a casting helper to cast the object to the correct type based on the hidden field.

2) I have a similar idea in some code, but I created a viewmetamodel class that contained all my types as nullable properties. My action method accepts this viewmetamodel type and validates the properties that are not null.

In line with this, if your data is not too large, then you could load all the settings tabs and use Jquery apply the tab with on click.

3) You could also create @sections or use EditorFor(c=>c.settings) for each tab. That way each tab will load a type safe object. You would need to create controllers for each.

I would say pick the easiest method for you. I hope that this at least gives you some ideas.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜