简体   繁体   English

控制器MVC c#的局部视图

[英]partial view with controller MVC c#

Hi i have a partial view that has a search form, the search form redirects to action "Search" in Controller "Search".. I am using this form in multiple locations but i need the action "Search" in Controller "Search" to pick up where this form is used to perform some actions 嗨,我有一个带有搜索表单的局部视图,该搜索表单重定向到控制器“搜索”中的操作“搜索”。.我在多个位置使用此表单,但是我需要控制器“搜索”中的操作“搜索”以在此表格用于执行某些操作的地方选择

_SearchForm (view) _SearchForm(视图)

  @using (Html.BeginForm("Search", "Search", FormMethod.Post)) { @Html.TextBox("querySTR") <input class="btn btn-primary btn-large" type="submit" value="Search" /> <label>@Html.RadioButton("rdList", "rdExact") Exact Term</label> <label>@Html.RadioButton("rdList", "rdStart", true) Start of Term</label> <label>@Html.RadioButton("rdList", "rdPart") Part of Term</label> } Controller 

@using (Html.BeginForm("Search", "Search", FormMethod.Post)) { @Html.TextBox("querySTR") <input class="btn btn-primary btn-large" type="submit" value="Search" /> <label>@Html.RadioButton("rdList", "rdExact") Exact Term</label> <label>@Html.RadioButton("rdList", "rdStart", true) Start of Term</label> <label>@Html.RadioButton("rdList", "rdPart") Part of Term</label> } Controller

 public ActionResult Search(FormCollection collection) { return RedirectToAction("Index", "Another Controller From where i am now"); } 

You can get the controller name in your view this way: 您可以通过以下方式在视图中获取控制器名称:

var controllerName = ViewContext.RouteData.Values["Controller"].ToString();

Now you can post the controller name with a hidden field in _SearchForm 现在,您可以在_SearchForm中发布带有隐藏字段的控制器名称

@{
    var controllerName = ViewContext.RouteData.Values["Controller"].ToString();
}

@using (Html.BeginForm("Search", "Search", FormMethod.Post))
{
    @Html.TextBox("querySTR")
    <input class="btn btn-primary btn-large" type="submit" value="Search" />
    <label>@Html.RadioButton("rdList", "rdExact") Exact Term</label>
    <label>@Html.RadioButton("rdList", "rdStart", true) Start of Term</label>
    <label>@Html.RadioButton("rdList", "rdPart") Part of Term</label>
    <input type="hidden" name="ControllerName" value="@controllerName" />

}

Then your SearchController can pick it up to redirect to the proper controller 然后,您的SearchController可以选择它以重定向到适当的控制器

public ActionResult Search(FormCollection collection)
{
    var controllerName = collection["ControllerName"];
    return RedirectToAction("Index", controllerName);
}

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

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