2011-09-11 27 views
1
問題

我SearchUsers查看看起來這樣:與MVC簡單搜索,以查看

<% foreach (var item in Model) 

我想要的模型與數據綁定:

<% using (Html.BeginForm()) 
    {%> 
<fieldset> 
    <legend>Fields</legend> 
    <div class="editor-label"> 
     <%= Html.Label("Search Users:" %> 
    </div> 
    <div class="editor-field"> 
     <%= Html.TextBox("keyword") %> 
    </div> 
    <p> 
     <input type="submit" value="Search" /> 
    </p> 
</fieldset> 
<%} %> 
<table> 
    <tr> 
     <th> 
      TITLE 
     </th> 
     <th> 
      FIRSTNAME 
     </th> 
     <th> 
      LASTNAME 
     </th> 
    </tr> 
    <% foreach (var item in Model) 
     { %> 
    <tr> 
     <td> 
      <%= Html.ActionLink("GetProfile", "User", new { username=item.USERNAME }) %> 
      | 
      <%= Html.ActionLink("UpdateProfile", "User", new { username=item.USERNAME })%> 
     </td> 
     <td> 
      <%= Html.Encode(item.TITLE) %> 
     </td> 
     <td> 
      <%= Html.Encode(item.FIRSTNAME) %> 
     </td> 
     <td> 
      <%= Html.Encode(item.LASTNAME) %> 
     </td> 
    </tr> 
    <% } %> 

一個例外是在那個去行拋出只有在我執行搜索後,但它似乎嘗試綁定數據時,我第一次訪問此視圖。我如何防止這種情況?

我的控制器是這樣的:

 public ActionResult SearchUsers() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult SearchUsers(FormCollection collection) 
    { 
     DBServiceLinq db = new DBServiceLinq(); 
     Acctinfo acct = new Acctinfo(); 
     acct = db.SearchUsers(collection["keyword"]); 
     return View(acct); 

    } 

我SearchUsers方法看起來如:

[WebMethod] 
    public Acctinfo SearchUsers(string keyword) 
    { 
     var q = from acctinfo in db.Acctinfos 
       where acctinfo.USERNAME.Contains(keyword) 
       select acctinfo; 
     Acctinfo a = new Acctinfo(); 
     a = q.First(); 
     return a; 

    } 
+0

LINQ延遲執行枚舉序列只有當模型被訪問,可能。 ToList()在控制器中? – bzlm

+0

添加了我的控制器/方法的外觀。我哪裏做錯了? @bzlm –

+0

如果您將其更改爲'acct = db.SearchUsers(collection [「keyword」])。ToList();'您將在控制器中發生錯誤。 [閱讀LINQ中的延遲執行*](http://www.codeguru.com/columns/vb/article.php/c16935)假設在視圖呈現時執行實際搜索這違背了模型,視圖和控制器責任的原則。 – bzlm

回答

1

假設exectipon爲空引用exeception,也許你設置視圖中的第一次:

public ActionResult SearchUsers() 
{ 
    return View(); 
} 

您的模型爲null。假設你的模式將是會在模型的構造函數來創建一個列表中有你你的觀點試圖

return (new MyModel()); 

反正只是在foreach之前,你可以檢查,如果模型爲空或不是

<% if(Model != null) { 
    foreach(....) 

更新:

而且你的方法是在反應後應返回一個列表

[HttpPost] 
public ActionResult SearchUsers(FormCollection collection) 
{ 
    DBServiceLinq db = new DBServiceLinq(); 
    IList<Acctinfo> acct = db.SearchUsers(collection["keyword"]); 
    return View(acct); 
} 


[WebMethod] 
public IList<Acctinfo> SearchUsers(string keyword) 
{ 
    var q = (from acctinfo in db.Acctinfos 
      where acctinfo.USERNAME.Contains(keyword) 
      select acctinfo).ToList();   
    return q; 
} 
+0

我之前嘗試過這種方式,並且我得到以下異常消息:傳入字典的模型項是類型'Practical4Redone.Acctinfo',但是該字典需要類型爲'System.Collections.Generic.IEnumerable'1 [Practical4Redone.Acctinfo]'的模型項目。 –

+1

所以你期待acctinfo列表。然後返回一個空的列表。 Return View(新列表()); – Iridio

+0

關於這一點,我的列標題顯示,但是當我點擊搜索時,我仍然得到相同的例外相比以前列列表甚至不會顯示。對不起,但我有點密集整個MVC概念/ facepalm –