Asp.Net MVC 4将搜索字符串添加为id参数

时间:2017-03-23 08:59:22

标签: c# asp.net asp.net-mvc-4

我创建了一个简单的网站,用户可以使用id搜索一些内容(总是只返回1个结果),我想将此搜索输入作为ID添加到URL。这样您就可以与此特定搜索结果共享URL。

看起来像

Mysite/ProductSearch/12345

我不知道如何将id传递给URL参数,并在链接条目上运行搜索此ID。

我很感激任何提示和参考。

Controller(ProductStateLookup):

[HttpPost]
public ActionResult Index(string Search)
{
    // some code to query the db and return me model with data 
    return View(model);
}

查看:

<div id="ProductSearch">
    <form action="ProductStateLookup/Index" method="POST">
        <input type="text" name="Search" placeholder="Enter Product ID">
        <input type="submit" value="Check P">
    </form>
</div>

1 个答案:

答案 0 :(得分:1)

您需要将方法设为 searchView.setOnSearchClickListener(new View.OnClickListener() { @Override public void onClick(View view) { searchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH); } }); 并将[HttpGet]更改为<form>

method="GET"

查看

public ActionResult Index(string Search)

请注意,这会向网址添加查询字符串(即@using (Html.BeginForm("Index", "ProductStateLookup", FormMethod.Get)) { <input type="text" name="Search" placeholder="Enter Product ID"> <input type="submit" value="Check P"> } ),除非您添加自定义路由定义,或将参数和文本框../ProductStateLookup/Index?search=1234属性更改为{{1} }。 自定义路由定义需要类似于

name

会生成id