简体   繁体   English

MVC4根据用户选择动态更改表单的操作

[英]MVC4 Dynamically changing a form's action based on user selection

I have a requirement to change a form's action depending on what selection a user makes on a dropdown box inside the form, I have an MVC4/C# project on .NET 4.5 我需要根据用户在表单内的下拉框中做出的选择来更改表单的操作,我在.NET 4.5上有一个MVC4 / C#项目

The choice that the user makes will influence which controller or which action needs to be called. 用户做出的选择将影响需要调用哪个控制器或哪个操作。

I have done a bit of searching around and I have come down to a few solutions: 我已经做了一些搜索,我已经找到了一些解决方案:

  • Implement JQuery client side to dynamically change the form's action 实现JQuery客户端以动态更改表单的操作
  • Have the form go to a specific action dedicated to routing the request to the proper controller/action 让表单转到专门用于将请求路由到适当的控制器/操作的特定操作

From what I have read, people have recommended against using JQuery since you can't be sure the client is running javascript, and similar issues. 根据我的阅读,人们建议不要使用JQuery,因为你无法确定客户端是否正在运行javascript,以及类似的问题。

I have been playing around with my second option here, I have been trying to use RedirectToAction to perform the routing inside a switch block, however it won't work with POST data since redirecting will cause a GET. 我一直在玩我的第二个选项,我一直在尝试使用RedirectToAction来执行交换机块内的路由,但是它不能用于POST数据,因为重定向会导致GET。

An example of what I've attempted inside the controller follows: 我在控制器内尝试的一个例子如下:

    [HttpPost]
    public ActionResult Index(TestObject object)
    {
        switch (object.Type) {

            case("A"):
                return RedirectToAction("ActionA");
            case ("B"):
                return RedirectToAction("ActionB");
            case ("C"):
                return RedirectToAction("ActionC");
            default:
                ModelState.AddModelError("", "Invalid Type");
                return View();
        }
    }

    [HttpPost]
    public ActionResult ActionA(TestObject object)
    {
            // Do Stuff for ActionA
            return View();
    }

This is the first MVC project I've created so I'm wondering what is considered the proper way if everyone keeps saying not to use JQuery for a situation like this. 这是我创建的第一个MVC项目,所以我想知道如果每个人都说不要在这样的情况下使用JQuery,那么正确的方法是什么。

If the second option is the 'proper' solution, how do I go about sending the data to the alternate controller actions? 如果第二个选项是“正确”的解决方案,我该如何将数据发送到备用控制器操作?

There's not currently any way to pass a model to RedirectToAction directly. 目前没有任何方法可以直接将模型传递给RedirectToAction This is what TempData is for: 这就是TempData的用途:

TempData["MyModel"] = model;

switch (object.Type) {
    case("A"):
        return RedirectToAction("ActionA");
    case ("B"):
        return RedirectToAction("ActionB");
    case ("C"):
        return RedirectToAction("ActionC");
    default:
        ModelState.AddModelError("", "Invalid Type");
        return View();
}

In your next action (note that this will be a GET request so you'll need to take the HttpPost annotation off it): 在您的下一个操作中(请注意,这将是一个GET请求,因此您需要从中获取HttpPost注释):

var model = TempData["MyModel"] as TestObject;

However, I'm a big fan of avoiding session storage wherever possible so, in this scenario, I wouldn't necessarily say that using jQuery is without any merit. 但是,我是尽可能避免会话存储的忠实粉丝所以,在这种情况下,我不一定会说使用jQuery没有任何优点。 The argument that "you can switch JavaScript off" is pretty moot nowadays given that the vast majority of modern sites are dependent on it. 鉴于绝大多数现代网站依赖于它,“你可以关闭JavaScript”这一论点现在已经没有实际意义了。

You could do something like the following, ie pass form values as routevalues: 您可以执行以下操作,即将表单值作为routevalues传递:

RedirectToAction("ACTIONNAME", "CONTROLLERNAME", ROUTEVALUES);

eg 例如

this.RedirectToAction("displayimport", "coilenquiry", new { Number = number });

then in your action, use the parameters... 然后在你的行动中,使用参数......

    public ActionResult DisplayImport(string Number)
    {

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM