我之前问过这个问题(如何在asp.net mvc视图中创建总计)。我认为最简单的方法是在模型中添加另一个项目,但我很好奇如何执行另一个使用Jquery的答案。在asp.net中使用jquery迭代表mvc
Showing totals in an asp.net MVC list view?
而且答案之一是迭代与jQuery表,并创建一个新的“总计”行。 我的jQuery技能是不存在的(我可以在我的视图中创建一个hello world)。任何建议都会很棒。也许有一个我应该看的有用的jQuery库。如果我也可以在这里添加一个排序会很好。
有人可以给我一个例子,说明这可能如何工作。
下面是一些示例asp.net MVC 2的代码,我想尝试的工作,这为
模型
namespace MvcScratch.Models
{
public class Thing
{
public string Name;
public int Value;
public Thing(string name, int value)
{
Name = name;
Value = value;
}
}
}
的控制器
public ActionResult Index()
{
List<Thing> myThings = new List<Thing>();
myThings.Add(new Thing("Thing 1",2));
myThings.Add(new Thing("Thing 2",3));
return View(myThings);
}
和视图(用jQuery hello world)。
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcScratch.Models.Thing>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<h2>Index</h2>
<table>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<%-- <td>
<%: Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> |
<%: Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%> |
<%: Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })%>
</td>--%>
<td>
<%: item.Name %>
</td>
<td>
<%: item.Value %>
</td>
</tr>
<% } %>
</table>
<p>
<%: Html.ActionLink("Create New", "Create") %>
</p>
<script type="text/javascript">
$(document).ready(function() {
$("h2").click(function() {
alert("Hello world!");
});
});
</script>
</asp:Content>
我个人的观点是在客户端(通过使用ajax调用获取数据并使用jQuery生成表)或者在服务器端进行。当你试图混合他们两个事情真的变得复杂。 – Ankur
@Ankur - 听起来不错。我想在这里学习一些东西。你使用http://www.datatables.net/创建你的表或其他东西吗? – Maestro1024
jquery完全是关于dom操作的。因此,在JQuery中,使用table tr和td元素创建表格的UI表示。如果你使用普通的javascript完成了DOM编程,那么Jquery会很容易学习 – Ankur