Reputation: 9252
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(); }); });
<div id="status" class="status" name="status">
<%--<% Html.RenderPartial("ClientIndex", ViewData["clients"]); %> Should this be here???? --%>
</div>
Upvotes: 0
Views: 8982
Reputation: 11
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.");
}
Upvotes: 1
Reputation: 10276
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()
Upvotes: 0