Asp.net mvc ajax更新div
控制器操作:Asp.net mvc ajax更新div,asp.net-mvc,asp.net-ajax,Asp.net Mvc,Asp.net Ajax,控制器操作: public ActionResult Index() { ViewData["sample"] = DateTime.Now.ToLongTimeString(); Thread.Sleep(3000); return View(); } 查看页面: <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server
public ActionResult Index()
{
ViewData["sample"] = DateTime.Now.ToLongTimeString();
Thread.Sleep(3000);
return View();
}
查看页面:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<div id="divId"><%= Html.Encode(ViewData["sample"])%></div>
<script type="text/javascript">var event = new Object(); $get("panelOneForm").onsubmit();</script>
<%using (Ajax.BeginForm("Index", "Proba", new AjaxOptions() { UpdateTargetId = "divId" }, new { id = "panelOneForm" })) { } %>
</asp:Content>
我尝试创建自动更新视图,但失败。怎么了?您是否导入了microsoft ajax库
<script src="/Content/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="/Content/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
这是一本书。你有萤火虫吗?如果是这样,请尝试查看库是否正确加载,以及在执行时是否检测到错误。您可以在表单中定义一个不可见的提交按钮:
<div id="divId"></div>
<%using (Ajax.BeginForm("Index", "Proba", new AjaxOptions() { UpdateTargetId = "divId" })) { %>
<input type="submit" id="btnSubmit" style="display:none;" />
<% } %>
<script type="text/javascript">
// Make sure you put this script after the form
// so that the button is loaded into the DOM before
// manipulating it
$get("btnSubmit").click();
</script>
我在MisrosoftAjax.debug.js上遇到此错误:Sys.ArgumentTypeException:类型为“Sys.\u Application”的对象无法转换为类型为“Sys.\u Application”。参数名称:实例[Break on this error]if!this.isInstanceofTypeInstance…e',Object.getTypeinstance,this;页面中是否有其他正在进行调用的ajax?你在web.config中引用了脚本吗?我在site.masterIn web.config中引用了脚本我有这样一个:奇怪的是,你在mvc中引用了这个脚本。我想不出来。我会使用jquery,很抱歉,我会尝试使用您的代码,但问题仍然存在,日期和时间不会自动更新$getbtnSubmit.click;只点击一次,why@Ognjen,可能是因为您只调用了一次。我将如何使用setInterval函数调整为每10秒调用一次ie自动刷新:window.setIntervalfunction{$getbtnSubmit.click;},10000;
public ActionResult Index()
{
// You probably want to remove the next line before shipping your
// application in production as it is not good to stall the thread for 3s
Thread.Sleep(3000);
return Content(DateTime.Now.ToLongTimeString(), "text/plain");
}