0

私はasp.netmvc3の初心者です。ユーザーが検索項目を入力し、データベースからデータを取得できるページを作成したいと思います。データベースを作成しました。私は次のコントローラーを作成しました:

public ActionResult SearchIndex(string id)
{
    string searchString=id;
    var search = from m in db.Searches
    select m;

    if (!String.IsNullOrEmpty(searchString))
    {
        search = search.Where(s => s.Name.Contains(searchString));
    }

    return View(search);
}

ビューページから上記のコントローラーに値を渡したい。上記のコントローラーから検索するアイテムを入力するためのビューページのコードは何ですか?

4

1 に答える 1

0

したがって、内部にフォームを使用してビューを作成します...次のようになります。

@using (Html.BeginForm())
{
    <label>Search:</label>
    <input type="text" name="searchString" />

    <input type="submit" name="submit" />
}

次に、コントローラーでFormCollectionオブジェクトを使用して、次のようにsearchStringを取得できます。

[HttpPost]
public ActionResult SearchIndex(FormCollection formCollection)
{
    string searchString = formCollection["searchString"];
    ...
}
于 2012-05-04T06:26:57.600 に答える