MVC - Ajax 表单 - 返回部分视图在
中不更新 目标

发布于 2024-07-25 15:13:13 字数 1652 浏览 1 评论 0原文

我有一个索引视图,我想在用户输入客户端 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>

主视图:

插入代码混乱,所以这只是复制/粘贴:

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

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

  </div>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹沉浮 2024-08-01 15:13:14

我有同样的问题。

在我的部分观点中,我有这个

<%@ 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.");
}

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.");
}
つ低調成傷 2024-08-01 15:13:14

与其不断在页面上发布表单,为什么不在幕后进行 jQuery.get 调用来获取所提供文本的搜索结果。 我认为这会更快更干净。 当像我认为的那样提交表单时(从我对代码的阅读来看),您会导致页面本质上刷新(而不是重新写入 div)。

$('#sForm').submit()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文