我有一个简单的表单,如下所示:
@using (Html.BeginForm("search", "home", new { param1 = "value1" }, FormMethod.Get ))
{
@Html.TextBox("Search", ViewBag.Search as string)
<button type="submit">Search </button>
}
HTML输出如下所示:
<form action="/home/search?param1=value1" method="get">
<input id="Search" name="Search" type="text" value="">
<button type="submit">Search </button>
</form>
和我的控制器看起来像这样:
public ActionResult Search(string param1, string search)
{
// param1 returns as "null"
// search contains the value that was inside the textbox
}
有人知道我做错了什么吗?
答案 0 :(得分:1)
一些建议
所以你的代码变成了:
public class SearchViewModel {
public string param1 {get;set;}
public string search {get;set;}
}
[HttpPost] // <-- were you missing this before?
public ActionResult Search(SearchViewModel model)
{
// access with model.param1
}
@model SearchViewModel
@using (Html.BeginForm("search", "home", FormMethod.POST ))
{
@Html.HiddenFor(x=>x.param1)
@Html.TextBoxFor(x=>x.search)
<button type="submit">Search </button>
}
答案 1 :(得分:0)
Selman22提出了实现这一目标的正确方法。您可以将它们放在隐藏的表单中,而不是将params放在实际的URL上。
@using (Html.BeginForm("search", "home", null, FormMethod.Get ))
{
<input type="hidden" name="param1" value="@ViewBag.Param1Value.ToString()" />
@Html.TextBox("Search", ViewBag.Search as string)
<button type="submit">Search </button>
}
谢谢!!!!