【发布时间】:2012-08-23 12:01:54
【问题描述】:
我正在从 VS2012RC 开发一个 Asp.net MVC3 网站。我正在尝试使用 Ajax Actionlink 创建网站的部分视图。但它不起作用。在与我的团队一起尝试了我所知道的一切并进行了大量搜索之后,我在一个 TestProject 上进行了尝试。我的进一步抑郁症也不起作用。我已经尝试解决这个问题很长时间了,现在我打电话给 SO。
这就是我创建测试项目的方式。请让我知道是我犯了错误还是这是我机器的故障。
- 我创建了一个带有 html5 语义标记和 Razor 视图引擎的 MVC3 空项目。
- 然后我在我的项目中添加了一个 Home 控制器并为 View() 操作创建了一个视图。
- 然后我创建了另一个动作 TestAjax()。
- 然后我在 Views 的 Home 文件夹中创建了一个局部视图 文件夹。
这是我的索引视图:
@{
ViewBag.Title = "Index";
}
<div>
@Ajax.ActionLink("This is a test ajax","testAjax",new AjaxOptions{ UpdateTargetId = "TestAjax" ,HttpMethod ="GET",InsertionMode =InsertionMode.InsertAfter})
</div>
<div id ="TestAjax">
</div>
部分视图是:
<h1>Testing testing testing testing</h1>
现在我期望的是,当我单击链接时,标题必须出现在 TestAjax 部门中的链接下方。但实际发生的是在主页中出现链接。当我单击链接时,它将转到“/Home/testAjax”并仅显示部分视图。没有局部视图。
你能告诉我这个程序有什么问题吗?
编辑:
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/modernizr-2.0.6-development-only.js")" type="text/javascript"></script>
<script src="@Url.Content(" ~/Scripts/jquery.unobtrusive-ajax.js")" type = "text/javascript"> </script>
</head>
当我在视图Index()的网页中查看资源时:
<a href="/Home/TestAjax" onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.insertAfter, httpMethod: 'GET', updateTargetId: 'TestAjax' });">This is a test ajax</a>
【问题讨论】:
-
点击链接时会发生什么?您是否在布局/视图中引用了
"~/Scripts/jquery.unobtrusive-ajax.js"js 文件?因为Ajax.ActionLink和Ajax.BeginForm需要这个js文件才能正常工作。 -
啊对不起!!!它将仅显示部分视图。我不好,我会回答这个问题。
-
我是否必须将这个引用插入到两个视图(索引,TestAjax)?
-
不,只能在你的主视图索引中或进入
_Layout.cshtml,但你不应该放入部分。 -
我在
_Layout.cshtml中添加了<script src="@Url.Content(" ~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>但是<script src="@Url.Content("~/Scripts/modernizr-2.0.6-development-only.js")" type="text/javascript"></script>there 有这个。我在索引视图中添加了Layout = "~/Views/Shared/_Layout.cshtml";,但结果是一样的。 :(
标签: c# razor asp.net-ajax visual-studio-2012