2

私は非常に単純なコードを持っています:

@using (Ajax.BeginForm("SearchAccount", "Account", new AjaxOptions { UpdateTargetId = "SearchResults", HttpMethod = "Get", InsertionMode = InsertionMode.Replace })) 
    {
        <fieldset>
            <input id="txtSearchBox" name="SearchString" type="text"  />
        </fieldset>
        <input type="submit" value="Search"  />
    }

そしてコントローラー側には次のコードがあります

public PartialViewResult SearchAccount(FormCollection formCollection)
    {
        try
        {
            string SearchString = formCollection["SearchString"];
            List<Moovers.DAL.Account> Accounts = Moovers.BL.Account.SearchAccount(SearchString);

            return PartialView("_AccountSearchResult", Accounts);        
        }
        catch (Exception ex)
        {
            throw;
        }

    }

問題は空の「FormCollection」です。考えられる理由は何ですか?

4

2 に答える 2

3

これは"GET"、メソッドとして使用しているためです。

https://stackoverflow.com/a/2265210/120955を参照してください

于 2012-07-17T20:11:49.047 に答える
0

なぜ使用したいのFormCollectionですか?SearchStringアクションパラメータとして直接持つことができますよね?

public PartialViewResult SearchAccount(string SearchString)
{
  try
  {
     var Accounts = Moovers.BL.Account.SearchAccount(SearchString);
     return PartialView("_AccountSearchResult", Accounts);        
  }
  catch (Exception ex)
  {
     throw;
  }
}

フォームから複数の値を渡す場合は、ビュー モデルを作成して作業を簡素化できます。

元。

public class SearchModel
{
   public string SearchString { get; set; }
   .. others
}

public PartialViewResult SearchAccount(SearchModel searchModel)
{
  ...
}

重要なことは、フォーム フィールドの名前がパラメータ/プロパティ名と一致する必要があることです。

于 2012-07-18T02:56:34.563 に答える