Jquery Ajax刷新视图的多个部分
我需要一个关于使用AJAX的建议。我正在设计一个视图,在主(上)部分显示表格数据。每行都有一个链接Jquery Ajax刷新视图的多个部分,jquery,ajax,asp.net-mvc,Jquery,Ajax,Asp.net Mvc,我需要一个关于使用AJAX的建议。我正在设计一个视图,在主(上)部分显示表格数据。每行都有一个链接Select,该链接将刷新/填充两个div容器,其中的数据与单击的链接Select对应 我知道我可以使用jQuery的$.load()或$.get()助手和$(“#选择”)来触发异步请求Ajax$.get()具有重载版本,可将对象路由VLUE发送到控制器。我想我需要一个JavaScript函数。有一件事我不知道该怎么解决,可能很容易做到。我需要以某种方式区分所有Select链接,以便它们可以执行相同
Select
,该链接将刷新/填充两个div容器,其中的数据与单击的链接Select
对应
我知道我可以使用jQuery的$.load()
或$.get()
助手和$(“#选择”)
来触发异步请求Ajax$.get()
具有重载版本,可将对象路由VLUE发送到控制器。我想我需要一个JavaScript函数。有一件事我不知道该怎么解决,可能很容易做到。我需要以某种方式区分所有Select
链接,以便它们可以执行相同的JavaScript函数,但请求不同的数据。我希望这是有道理的。提供此区别的值是ID
因此,我将有20个select链接,每个链接都应该执行该函数,根据不同的ID发送请求,以HTML、JSON等形式获取数据,然后用相应的数据刷新div容器
我不知道我是否应该在一个PartialView中使用PartialView或JSON或PartialView
我该怎么分类呢
PS>我想我可以使用@Html.ActionLink(0
来避免AJAX中的硬编码字符串
编辑: 假设我有数据:
Number Start End Target Usage
A0000 24/11/2014 27/11/2014 Non-commercial null Select
A0000 02/05/2013 29/05/2013 Academic Student00 Select
A0000 28/12/2013 17/02/2014 Non-commercial Student00 Select
A0000 16/04/2014 27/01/2014 Non-commercial null Select
A0000 15/02/2014 14/01/2014 Academic Student00 Select
A0000 06/08/2014 14/02/2014 Academic null Select
A0000 08/11/2014 04/03/2014 Non-commercial null Select
A0000 26/05/2014 13/04/2014 Academic Student00 Select
最后一列包含填充另外两个div
容器的链接。当我单击Select
时,我想在一个容器中获取StudentXX
的详细信息,比如说div1
和div2
中分配的所有模块。如果我有一个部分视图要填充,我会使用@Ajax.ActionLink(“选择”,“详细信息”“学生”,
您可以使用jQuery.data()
存储和检索带有元素的任意数据。假设您的模型是IEnumerable
,并且Student
具有属性StudentID
,则在您的视图中
<table>
....
<tbody>
@foreach(var item in Model)
{
<tr>
// other table cells
<td><button type="button" class="details" data-id="@item.StudentID">Select</button><td>
...
<tr>
}
</tbody>
</table>
<div id="studentdetails"></div>
<div id="studentmodules"></div>
控制器方法在哪里
public PartialViewResult Details(int ID)
{
var model = // get the student details based on ID
return PartialView(model);
}
public PartialViewResult Modules(int ID)
{
var model = // get the student modules based on ID
return PartialView(model);
}
注意,我使用了
按钮而不是元素(您不需要导航),但是如果您需要,可以很容易地将其样式设置为链接。使用jQuery.load()
而不是@Ajax.ActionLink()
意味着您可以删除jquery.unobtrusive ajax.js
文件。您需要发布视图或一些html,以便我们可以understand@CerlinBoss,那是PHP,不是MVC!它真的不是matter@ZikO,可以添加为表的一行生成的html吗(这样我就可以得到正确的jquery选择器)。如果两个方法都有相同的参数(studentID
)有什么原因不能合并(以JSON或部分视图的形式返回学生详细信息及其模块)?工作正常。非常感谢:)不管是按钮还是链接。我稍后会整理presentationm样式和所有这些。干杯!
<table>
....
<tbody>
@foreach(var item in Model)
{
<tr>
// other table cells
<td><button type="button" class="details" data-id="@item.StudentID">Select</button><td>
...
<tr>
}
</tbody>
</table>
<div id="studentdetails"></div>
<div id="studentmodules"></div>
$('.details').click(function() {
var studentID = $(this).data('id'); // get the student id associated with the element
$('#studentdetails').load('@Url.Action("Details", "Student")', { id: studentID });
$('#studentmodules').load('@Url.Action("Modules", "Student")', { id: studentID });
});
public PartialViewResult Details(int ID)
{
var model = // get the student details based on ID
return PartialView(model);
}
public PartialViewResult Modules(int ID)
{
var model = // get the student modules based on ID
return PartialView(model);
}