问题
I have table, that is linked with model.
@foreach (var item in Model)
{
<tr onclick="window.location.href = '@Url.Action("Action", new RouteValueDictionary { { "id", item.Id } })'">
<td>@Html.DisplayFor(x => item.Field1)
</td>
<td>@Html.DisplayFor(x => item.Field2)
</td>
</tr>
}
How can i update with ajax my partial view, for edit selected model from table.
I deleted onclick="window.location.href = '@Url.Action("Action", new RouteValueDictionary { { "id", item.Id } })'"
and added script:
$(function () {
$('tr').click(function () {
$.ajax({
url: '@Url.Action("Action", new RouteValueDictionary { { "id", ??? } })',
type: 'GET',
cache: false,
success: function (result) {
$('#partialView_div').html(result);
}
});
return false;
});
});
But, i don't now, how to pass Model.Id
into this script.
回答1:
You can use attributes to save item.Id
and fetch it in event handler. Additionally you don't need to use window.location.href
CSHTML
@foreach (var item in Model)
{
<tr class="deleteItem" data-url="@Url.Action("Action", new RouteValueDictionary { { "id", item.Id } })">
<td>@Html.DisplayFor(x => item.Field1)
</td>
<td>@Html.DisplayFor(x => item.Field2)
</td>
</tr>
}
JavaScript
$('tr.deleteItem').click(function () {
$.ajax({
url: $(this).data("url"),
type: 'GET',
cache: false,
success: function (result) {
$('#partialView_div').html(result);
}
});
return false;
});
OR
CSHTML
@foreach (var item in Model)
{
<tr data-item-id="@item.Id" class="deleteItem">
<td>@Html.DisplayFor(x => item.Field1)
</td>
<td>@Html.DisplayFor(x => item.Field2)
</td>
</tr>
}
JavaScript
$('tr.deleteItem').click(function () {
var url = '@Url.Action("Action", "Controller", { "id" = "???" })';
url = url.replace("???", $(this).data("item-id"));
$.ajax({
url: url,
type: 'GET',
cache: false,
success: function (result) {
$('#partialView_div').html(result);
}
});
return false;
});
回答2:
I will try to make this simple
@foreach (var item in Model)
{
<tr data-item-id="@item.Id">
<td>@Html.DisplayFor(x => item.Field1)
</td>
<td>@Html.DisplayFor(x => item.Field2)
</td>
</tr>
}
jQuery
$(function () {
$('tr').click(function () {
$.ajax({
url: 'controller/action/',
type: 'GET',
cache: false,
data:{id:$(thi).data("item-id")},
success: function (result) {
$('#partialView_div').html(result);
}
});
return false;
});
});
回答3:
Please follow below step so you will get your required output. Main View
@model IEnumerable<MvcApplication2.Models.Test>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('tr.delete').click(function () {
var url = '@Url.Action("Test1", "Home")';
var Itemid = $(this).data("item-id");
$.ajax({
type: "POST",
url: url,
contentType: 'application/json; charset=utf-8',
data: "{'itemid' : " + Itemid + "}",
dataType: 'json',
cache: false,
success: function (result) {
$('#divpartial').html(result);
}
});
return false;
});
});
function printperson(div, item) {
div.append("<br/>" + "FName: " + item.FirstName + " , LName: " + item.LastName);
$.each(item.Addresses, function (i, addr) { printaddress(div, addr); });
}
function printaddress(div, item) {
div.append("<br/>" + "Line1: " + item.Line1);
}
</script>
<table border="1">
@foreach (var item in Model)
{
<tr data-item-id="@item.Id" class="delete">
<td>
@item.Id
</td>
<td>
@item.Field1
</td>
<td>
@item.Field2
</td>
</tr>
}
<div id="divpartial">
</div>
</table>
Controller
[HttpGet]
public ActionResult Index()
{
List<Test> obj = new List<Test> {
new Test{Id=1,Field1="Field11",Field2="Field21"},
new Test{Id=2,Field1="Field12",Field2="Field22"},
new Test{Id=3,Field1="Field13",Field2="Field23"},
};
return View(obj);
}
public ActionResult Test1(int itemid)
{
PartialViewModel obj = new PartialViewModel();
obj.Id = itemid;
return PartialView("~/Views/Partial1.cshtml", obj);
}
Model
public class Test
{
public int Id;
public string Field1;
public string Field2;
}
public class PartialViewModel
{
public int Id;
}
Partial View
@model MvcApplication2.Models.PartialViewModel
@Model.Id
来源:https://stackoverflow.com/questions/21043392/how-to-refresh-partial-view-after-table-row-click