繁体   English   中英

控制器MVC c#的局部视图

[英]partial view with controller MVC c#

嗨,我有一个带有搜索表单的局部视图,该搜索表单重定向到控制器“搜索”中的操作“搜索”。.我在多个位置使用此表单,但是我需要控制器“搜索”中的操作“搜索”以在此表格用于执行某些操作的地方选择

_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"); } 

您可以通过以下方式在视图中获取控制器名称:

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

现在,您可以在_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" />

}

然后,您的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