MVC - Ajax 表单 - 返回部分视图在中不更新 目标
我有一个索引视图,我想在用户输入客户端 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有同样的问题。
在我的部分观点中,我有这个
,但应该是
测试 ajax 调用是否正常工作的一种简单方法是返回一个字符串而不是 ActionResult
I had the same problem.
In my partial view, I had this
but it should have been this
an easy way to test if your ajax call is working is to return a string instead of an ActionResult
与其不断在页面上发布表单,为什么不在幕后进行 jQuery.get 调用来获取所提供文本的搜索结果。 我认为这会更快更干净。 当像我认为的那样提交表单时(从我对代码的阅读来看),您会导致页面本质上刷新(而不是重新写入 div)。
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).