MVC2 Action处理多个模型 [英] MVC2 Action to handle multiple models

查看:90
本文介绍了MVC2 Action处理多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这个问题的答案,我不敢相信从来没有问过这个问题,但是我没有运气在这里尝试.

I've been looking around for an answer to this, which I can't believe hasn't been asked before, but with no luck I'm attempting here.

我有一个注册表格,该表格根据请求者是哪种类型的参与者而略有不同.在为解决方案编写测试时,我意识到所有动作都执行相同的操作,因此我试图使用一种策略模式将这些动作组合为一个动作.

I have a signup form which differs slightly based upon what type of participant the requester is. While writing tests for the solution, I realized that all actions did the same things, so I'm attempting to combine the actions into one using a strategy pattern.

public abstract class BaseForm { common properties and methods }
public class Form1 : BaseForm { unique properties and overrides }
....
public class FormX : BaseForm { unique properties and overrides... in all about 5 forms }

这是组合动作:

[ModelStateToTempData, HttpPost]
public ActionResult Signup(int id, FormCollection collection)
{
    uiWrapper= this.uiWrapperCollection.SingleOrDefault(w => w.CanHandle(collection));
    // nullcheck on uiWrapper, redirect if null
    var /*BaseForm*/ form = uiWrapper.GetForm();  // Returns FormX corresponding to collection.
    this.TryUpdateModel(form, collection.ToValueProvider()); // Here is the problem
    form.Validate(this.ModelState);  // Multi-Property validation unique to some forms.
    if (!this.ModelState.IsValid)
        return this.RedirectToAction(c => c.Signup(id));

    this.Logic.Save(form.ToDomainClass());
    return this.RedirectToAction(c => c.SignupComplete());
}

我遇到的问题是TryUpdateModel仅绑定在BaseForm中找到的公共属性.我之前的代码使用了正确绑定的公共ActionResult FormName(int id,FormX form).但是,我进行了一些测试,发现如果我用FormX替换var form,则窗体绑定并且一切正常,但是我回到每种窗体类型的一个动作.

The problem I'm having is that TryUpdateModel binds only the common properties found in BaseForm. My previous code used public ActionResult FormName(int id, FormX form) which bound properly. However, I did some testing and discovered that if I replace var form with FormX form the form binds and everything works, but I'm back to one action per form type.

我希望找到一种方法来使其正确绑定. form.GetType()返回正确的表单非基类,但是我不确定如何获取构造函数,实例化一个类,然后将其扔到TryUpdateModel中.我知道另一种可能是自定义ModelBinder,但是我看不到没有遇到相同FormBase问题的创建方式.

I'm hoping to find a way to get this to bind properly. form.GetType() returns the proper non-base class of the form, but I'm not sure of how to grab the constructor, instantiate a class, and then throw that into TryUpdateModel. I know that the other possibility is a custom ModelBinder, but I don't see a way of creating one without running into the same FormBase problem.

有什么想法或建议去找哪里?

Any ideas or suggestions of where to look?

推荐答案

我知道了!

Erik的答案不是解决方案,但由于某种原因,它使我想到了解决方案.

Erik's answer wasn't the solution, but for some reason it made me think of the solution.

我真正希望表单成为动态类型.如果我更改此行:

What I really want form to be is a dynamic type. If I change this line:

dynamic form = uiWrapper.GetForm();

一切正常:)

最重要的是,logic.Save(form.ToDomainClass())也直接转到Save(DomainTypeOfForm)而不是Save(BaseDomainForm),所以我也可以避免麻烦.我知道,一旦在这里找到问题,我也可以在逻辑课中应用答案.

On top of that, logic.Save(form.ToDomainClass()) also goes directly to Save(DomainTypeOfForm) rather than Save(BaseDomainForm) so I can avoid the headache there as well. I knew that once I figured out the problem here I could apply the answer in my logic class as well.

这篇关于MVC2 Action处理多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆