【问题标题】:Ajax in ASP.NET MVC redirect to partial viewASP.NET MVC 中的 Ajax 重定向到部分视图
【发布时间】:2019-05-10 15:06:39
【问题描述】:

我在 ajax 和 ASP.NET MVC 中遇到问题。当我单击按钮提交时,我重定向到部分页面。我不需要重定向 - 我需要替换 div

控制器

public PartialViewResult ajaxtest(string prname)
{
    var aj = db.acount.Where(p => p.name.StartsWith(prname));
    return PartialView("_ajTest", aj.ToList());
}

查看

@using (Ajax.BeginForm("ajaxtest", "Default1", new AjaxOptions () {
     HttpMethod = "POST",
     UpdateTargetId = "othmanN",
     InsertionMode = InsertionMode.Replace 
}))
{
   <label>Product Name :</label>
   @Html.TextBox("prname")

   <input type="submit" name="name" value="search" />
}
<div id="othmanN">
//here table 
</div>

【问题讨论】:

  • 你尝试过使用 JQuery 并在成功函数中替换 Html 吗?
  • 您可以使用此网址stackoverflow.com/questions/4730777/…将部分视图作为字符串返回
  • 我不需要转换成字符串
  • 当它作为字符串返回时,您可以使用 html 字符串到 html -replace div - $("container selector").html(returned html);
  • 您的页面中是否包含jquery.unobtrusive-ajax.js javascript 文件?如果您看到的是普通表单提交而不是 ajax,这意味着所需的脚本未正确加载。检查您的浏览器控制台,看看您是否在那里看到任何错误。

标签: ajax asp.net-mvc


【解决方案1】:

你的代码没问题,只需要导入 jquery.unobrusive-ajax.js

jquery 导入后导入

<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>

如果你没有安装 de lib,它在这里: https://www.nuget.org/packages/Microsoft.jQuery.Unobtrusive.Ajax/

【讨论】:

  • 你能打开 chrome 开发工具 (f12) 并检查控制台中的任何错误吗?
猜你喜欢
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 2013-01-18
  • 2017-06-14
  • 2011-11-10
  • 1970-01-01
  • 2012-06-22
  • 2019-01-18
相关资源
最近更新 更多