views:

1625

answers:

2

I have an index view that I want to update automatically as the user types in a client id. I got something similiar to work (only it was updating just a label) - but this will not work.

What happens is the partial is just rendered by itself (not in place of the UpdateTargetID). So the data is rendered on a new page. Here is my code:

Controller:

public ActionResult ClientList(string queryText)
    {
        var clients = CR.GetClientLike(queryText);
        return PartialView("ClientIndex", clients);
    }

Partial View:

<table>
<thead>
    <tr>
        <td>Client ID</td>
        <td>Phone1</td>
        <td>Phone2</td>
        <td>Phone3</td>
        <td>Phone4</td>
    </tr>
</thead>
<tbody>
    <% if (Model != null)
       {
           foreach (Client c in Model)
           { %>
        <tr>
            <td><%= Html.Encode(c.ClientID)%></td>
            <td><%= Html.Encode(c.WorkPhone)%></td>
            <td><%= Html.Encode(c.WorkPhone1)%></td>
            <td><%= Html.Encode(c.WorkPhone2)%></td>
            <td><%= Html.Encode(c.WorkPhone3)%></td>

        </tr>
    <% }
       } %>
</tbody>

Main View:

Insert code messed up, so this is just copy/pasted:

$(function() { $("#queryText").keyup(function() { $('#sForm').submit(); }); });

<% using (Ajax.BeginForm("ClientList", /* new { queryText = Form.Controls[2] ?? }*/"", new AjaxOptions { UpdateTargetId = "status", InsertionMode = InsertionMode.Replace }, new { @id = "sForm" })) { %>

<% } %>

  <div id="status" class="status" name="status">
    <%--<% Html.RenderPartial("ClientIndex", ViewData["clients"]); %> Should this be here???? --%>

  </div>
A: 

Rather than posting a form on the page constantly why not do a behind the scenes jQuery.get call to get the search results for the provided text. I would think this would be faster and cleaner. When submitting the form like I think you are (judging from my reading of your code) you are causing the page to essentially refresh (and not re-write to the div).

$('#sForm').submit()
Andrew Siemer
How might I go about doing that? I've seen some examples of basic jQuery.get usage, but what is the syntax for passing the form data (from the text box)? Is it just simply $("#queryText").val(); ? I have not messed with json results at all.
Jack
Andrew Siemer
+1  A: 

I had the same problem.

In my partial view, I had this

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

but it should have been this

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IQueryable<Client>>" %>


an easy way to test if your ajax call is working is to return a string instead of an ActionResult

public string ClientList(string queryText)
{
   return("ok, the ajax call must have worked because I see this string.");
}

Craig Howard