Gor*_*anB 10 asp.net jquery razor asp.net-mvc-3
在我的mvc项目中,我有一个简单的项目列表,其中包含如下所示的crud操作:
<tbody>
@{
foreach (var item in Model)
{
<tr>
<td>@item.Title</td>
<td>@item.Body</td>
<td>@item.Price</td>
<td><span class="EditLink ButtonLink" noteid="@item.Id">Edit</span> | <span>@Html.ActionLink("Delete", "Delete", new { id = @item.Id})</span>
| @Html.ActionLink("Detalji", "Details", new { id = @item.Id})
</td>
</tr>
}
}
</tbody>
Run Code Online (Sandbox Code Playgroud)
我想知道当我点击详细信息时,可以将详细信息视图呈现为表格下的部分视图.我的意思是当我clik细节向我展示细节视图时,我希望它在我的桌子下面的某个div或段落中.
请帮忙.
Dar*_*rov 22
你可以使用AJAX.但首先让我们通过摆脱这些循环并用显示模板替换它们来改进代码:
@model IEnumerable<SomeViewModel>
<table>
<thead>
<tr>
<th>Title</th>
<th>Body</th>
<th>Price</th>
<th>actions ...</th>
</tr>
</thead>
<tbody>
@Html.DisplayForModel()
</tbody>
</table>
<div id="details"></div>
Run Code Online (Sandbox Code Playgroud)
然后定义一个显示模板(~/Views/Shared/DisplayTemplates/SomeViewModel.cshtml):
@model SomeViewModel
<tr>
<td>@Html.DisplayFor(x => x.Title)</td>
<td>@Html.DisplayFor(x => x.Body)</td>
<td>@Html.DisplayFor(x => x.Price)</td>
<td>
<!-- no idea what the purpose of this *noteid* attribute on the span is
but this is invalid HTML. I would recommend you using the
HTML5 data-* attributes if you wanted to associate some
metadata with your DOM elements
-->
<span class="EditLink ButtonLink" noteid="@Model.Id">
Edit
</span>
|
<span>
@Html.ActionLink("Delete", "Delete", new { id = Model.Id })
</span>
|
@Html.ActionLink(
"Detalji", // linkText
"Details", // actionName
null, // controllerName
new { id = Model.Id }, // routeValues
new { @class = "detailsLink" } // htmlAttributes
)
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
现在剩下的就是在单独的javascript文件中AJAXify这个详细信息链接:
$(function() {
$('.detailsLink').click(function() {
$('#details').load(this.href);
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
当然,您假设您有以下行动:
public ActionResult Details(int id)
{
SomeDetailViewModel model = ... fetch the details using the id
return PartialView(model);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27012 次 |
| 最近记录: |