MVC - Ajax表单 - 返回部分视图不会在<div> target </div>中更新

时间:2009-07-03 00:14:09

标签: asp.net-mvc asp.net-ajax asp.net-mvc-partialview

我有一个索引视图,我想在用户输入客户端ID时自动更新。我得到了类似于工作的东西(只是它只更新了标签) - 但这不起作用。

部分只是自己呈现(不代替UpdateTargetID)。因此,数据将在新页面上呈现。这是我的代码:

控制器:

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

部分视图:

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

主要观点:

插入代码搞砸了,所以这只是复制/粘贴:

             $(function(){             $(“#queryText”)。keyup(function(){                 $( '#sForm')提交()。             });         });     

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

  </div>

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。

在我的部分视图中,我有这个

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

但应该是这个

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

测试ajax调用是否正常的简单方法是返回字符串而不是ActionResult

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

答案 1 :(得分:0)

不是在页面上不断发布表单,为什么不在幕后进行jQuery.get调用以获取所提供文本的搜索结果。我认为这会更快更清洁。提交表单时,我认为你是(从我的代码读取来判断)你导致页面基本上刷新(而不是重写到div)。

$('#sForm').submit()
相关问题