问题描述:

i have a very simple code:

@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" />

}

and on controller side i have following code

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;

}

}

the problem is "FormCollection", which is empty. What could be the possible reason ?

网友答案:

This is because you're using "GET" as your method.

See http://stackoverflow.com/a/2265210/120955

网友答案:

Why you want to use FormCollection? You can directly have the SearchString as the action parameter right?

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

If you are passing multiple values from Form then you can create a view model and simplify your life.

Ex.

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

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

Note, the important thing is the names of the form fields should match the parameter/property names.

相关阅读:
Top