0

リンクからの検索機能を作りたい。

現在、ホームページに検索ボックスがあります。検索ボックスを動作させています。

ただし、いくつかのパラメーターをコントローラーの Search メソッドに渡す方法がわかりません。

このようにコーディングすると、コントローラーで searchString が「null」になります。

actionLink を介して serachString パラメータを取得するにはどうすればよいですか?

私たちを手伝ってくれますか?たぶん簡単に見えます。私を助けてください、または私にアドバイスしてください。ありがとう。

//mac.cshtml

<h3>CPU Processor</h3>
<ul>
    <li>@Html.ActionLink("Intel Core i5", "Search", "Store", new { @searchString = "i5"})</li>
    <li>@Html.ActionLink("Intel Core i7", "Search", "Store", new { @searchString = "i7" })</li>
</ul>

//Search method in StoreController
public ActionResult Search(string searchString)
    {

        var product = from a in _db.Product.Include(a => a.Category)
                      select a;
        if (!String.IsNullOrEmpty(searchString))
        {
            product = product.Where(a => a.model.ToUpper().Contains(searchString.ToUpper())
                               || a.Category.name.ToUpper().Contains(searchString.ToUpper()));
        }
        return View(product.ToList());
    }
4

1 に答える 1

1

Html.ActionLink に適切なオーバーロードを使用していません

見る

http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink.aspx

やったほうがいい

@Html.ActionLink("Intel Core i7", "Search", "Store", new { @searchString = "i7" }, null)

パラメータとしての「オブジェクト」と混同されることがよくあります。

ちなみに、このメソッドは危険(パラメータを取得)で、linq2entities では ToUpper メソッドは機能しません。

于 2012-04-12T15:55:23.297 に答える