开发者

Simple search with MVC, problems with View

My SearchUsers View looks as such :

    <% 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>
    <% } %>

An exception is thrown at the line that goes :

<% foreach (var item in Model)

I want the model to bind with data only after I perform a search, but it seems to try to bind data when I first access this vi开发者_高级运维ew. How do I prevent this?

My Controller is as such :

        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);

    }

My SearchUsers Method looks as such :

    [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;

    }


Assuming the exectipon is a null reference exeception, maybe the first time you set the view from:

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

Your model is null. Assuming your model would be a List that would be created in the constructor of your model have you tried

return (new MyModel());

Anyway in your view just before the foreach you can check if model is null or not

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

Update:

Also your method that react to the post should return a list

[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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜