嗨我有一个部分视图,有一个搜索表单,搜索表单重定向到控制器“搜索”中的操作“搜索”.. 我在多个位置使用此表单但我需要在“搜索”中使用“搜索”操作来获取此表单用于执行某些操作的位置
_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>
}
答案 0 :(得分:1)
您可以通过以下方式获取控制器名称:
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);
}