得到partialview行动父控制器名称 [英] Get the parent controller name from partialview action

查看:99
本文介绍了得到partialview行动父控制器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个叫搜索partialview。我想把这个局部视图中的许多观点。
目标是从搜索控制器采取搜索字符串输入,并传送给其中使用搜索视图父控制器

I have a partialview called search. I want to put this partial view in many view. The target is to take the search string input from the search controller and send it to the parent controller where the search view is used.

在这样,我想使搜索局部视图通用的,这样我就可以重新使用它。

In this way I want to make the search partial view generic so that I can re-use it.

SearchController:

SearchController:

 [HttpPost]
    public ActionResult Index(string searchString)
    {
        var controller = RouteData.Values.First().Value.ToString(); // this gives me "Search", which i dont want.

     //here i want to take the parent controller name and redirect to that controller

        return RedirectToAction("action", "controller", new { searchString = searchString });
    }

谁能帮我找到父控制器名字??

can anyone help me to find the parent controller name ??

推荐答案

而不是有SearchController的,你可以为你的控制器基类,写code,将它们之间共享。这是有道理的,如果需要在多个控制器的功能。

Instead of having a SearchController, you can make a base class for your controllers and write code that will be shared between them. It makes sense if your feature is needed in multiple controllers.

让我们假设你有一个基类为你的控制器:

Let's say your have a base class for your controllers :

public class BaseController : Controller
{
    [HttpPost]
    public ActionResult Search(string searchString)
    {
        // ... some process

        return RedirectToAction("SomeAction", new { searchString = searchString });
    }

    public virtual ActionResult SomeAction(string searchString)
    {
        // ... some other process
    }
}

那么你的特定的控制器:

Then your specific controller :

public class MyController : BaseController
{
    public override ActionResult SomeAction(string searchString)
    {
        // ... again some process
    }

    // .. some other actions
}

您partialview搜索将针对电流控制器,而不是SearchController(在您的视图不指定控制器的名称),所以你的RedirectToAction也将重定向到控制器的动作,而不必让自己的名字(这就是为什么有一个在code以上剪断没有控制器的名称)。

Your partialview "Search" will target current controller instead of "SearchController" (by not specifying controller name in your view), so your RedirectToAction will also redirect to an action of that controller, without having to get his name (that's why there's no controller name in the code snipper above).

而不是有一个虚拟的方法,你也可以通过一个字符串变量作为动作的名称,如果你需要它命名不同,根据电流控制器(它可以成为另一个参数,沿搜索字符串参数):

Instead of having a virtual method, you can also pass a string variable as the action name, if you need to name it differently according to current controller (it can become another parameter, along the searchString parameter) :

public class BaseController : Controller
{
    [HttpPost]
    public ActionResult Search(string searchString, string targetAction)
    {
        // ... some process

        return RedirectToAction(targetAction, new { searchString = searchString });
    }
}

如果你不想去同一个基类,你可以随时在您的视图获得当前控制器的名称,触发你的搜索功能前:

If you don't want to go with a base class, you can always get current controller name in your view, before triggering your search feature :

@HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()

在您的控制器,它变成了:

In your controller, it becomes :

[HttpPost]
public ActionResult Search(string searchString, string controllerName)
{
    // ... some process

    return RedirectToAction("action", controllerName, new { searchString = searchString });
}

但是要一个基类是使这种功能的通用和可重复使用的好办法。

But going for a base class is a good way to make this kind of feature generic and reusable.

这篇关于得到partialview行动父控制器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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