2011-12-02 14 views
0

データベースに請求書を検索する検索ボックスを作成しようとしています。検索ボックスのコードは以下の通りです:検索テキストボックスにnull値を受け取る

@using (Ajax.BeginForm("Search", "Invoice", new AjaxOptions() { HttpMethod = "POST" })) 

{ 

    <% input id="search-field" name="search" type="text" value="" %/> 

    <% input id="search-submit" name="search-submit" type="submit" value="" %/> 

} 

public ActionResult Search(FormCollection collection) 
{ 

     if (collection["search-field"] == null) 
      return RedirectToAction("Index"); 
     else 
     { 
      string id = collection["search-field"].ToString(); 
      return RedirectToAction("Details", "Invoice", id.Trim()); 
     } 
} 

今の問題は、私はコントローラ検索アクションのみヌル値を受け取るということです。

私は次のアクションに文字列をキャッチしながら、私はまだ文字列値を受け取ることができませんMVC3および.NET Framework 4.0

を使用しています:

公共のActionResult詳細(文字列ID) {

if(string.IsNullOrEmpty(id)) 

    return RedirectToAction("Index"); ==============> Here 

    ObjectParameter[] parameters = new ObjectParameter[3]; 

    parameters[0]= new ObjectParameter("CUSTNMBR", id); 
    parameters[1] = new ObjectParameter("StartDate", System.DateTime.Now.Date.AddDays(-90)); 
    parameters[2] = new ObjectParameter("EndDate", System.DateTime.Now.Date); 

    return View(_db.ExecuteFunction<Models.Invoices>("uspGetCustomerInvoices", parameters).ToList<Models.Invoices>()); 

答えて

1

主な問題は、に基づいてFormCollectionを検索していることですname属性の代わりにinput要素のこのようなあなたのコードを書いてみます

ビュー:

@using (Ajax.BeginForm("Search", "Invoice", new AjaxOptions() { HttpMethod = "POST" })) 

{ 

    <input id="search-field" name="search" type="text" value="" /> 

    <input id="search-submit" name="search-submit" type="submit" /> 

} 

処置:

public ActionResult Search(string search) 
{  
    if (string.IsNullOrEmpty(search)) 
     return RedirectToAction("Index"); 
    return RedirectToAction("Details", "Invoice", search.Trim()); 
} 

あなたはもはやFormCollection

を照会する必要があるので、私はあなたの行動を変更しません
関連する問題