【发布时间】:2013-12-15 19:12:28
【问题描述】:
我有这两个控制器动作:
public ActionResult Index(string search, int? page)
{
return View(db.powners.Where(x => x.petowner.StartsWith(search) || search == null).OrderBy(x => x.petowner).ToList().ToPagedList(page ?? 1, 5));
}
public ActionResult Ownerfind(string search, int? page)
{
return View(db.powners.Where(x => x.petowner.StartsWith(search) || search == null).OrderBy(x => x.petowner).ToList().ToPagedList(page ?? 1, 5));
}
还有这个json方法:
public JsonResult Getowner(int nownerid)
{
OwnerEntities owner = new OwnerEntities();
var existingCust = owner.powners.Single(p => p.ownerid == nownerid);
return Json(existingCust);
}
索引视图:
@model PagedList.IPagedList<Mvc4test2.Models.powner>
@using PagedList.Mvc;
@using PagedList;
@{
ViewBag.Title = "Index";
}
<link href="../../Content/PagedList.css" rel="stylesheet" type="text/css" />
<div style="font-family:Arial">
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<p>
@using (@Html.BeginForm("Index", "owner", FormMethod.Get))
{
<b>Search</b>@Html.TextBox("search")<input type="submit" value="search" />
}
</p>
<table id="ownertable">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().petowner)
</th>
<th>
@Html.DisplayNameFor(model => model.First().ostreet)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
<a href=""> @Html.DisplayFor(modelItem => item.ownerid) </a>
</td>
<td>
@Html.DisplayFor(modelItem => item.petowner)
</td>
<td>
@Html.DisplayFor(modelItem => item.ostreet)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ownerid }) |
@Html.ActionLink("Details", "Details", new { id=item.ownerid }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ownerid })
</td>
</tr>
}
</table>
@Html.PagedListPager(Model, page=>Url.Action("Index", new{page, search=Request.QueryString["search"]}), new PagedListRenderOptions(){Display=PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation=true,MaximumPageNumbersToDisplay=5})
所有者查找视图:
@model PagedList.IPagedList<Mvc4test2.Models.powner>
@using PagedList.Mvc;
@using PagedList;
@{
ViewBag.Title = "Index";
}
<link href="../../Content/PagedList.css" rel="stylesheet" type="text/css" />
<div style="font-family:Arial">
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<p>
@using (@Html.BeginForm("ownerfind", "owner", FormMethod.Get))
{
<b>Search</b>@Html.TextBox("search")<input type="submit" value="search" />
}
</p>
<p>
hello
</p>
<table id="ownertable">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().petowner)
</th>
<th>
@Html.DisplayNameFor(model => model.First().ostreet)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
<a href=""> @Html.DisplayFor(modelItem => item.ownerid) </a>
</td>
<td>
@Html.DisplayFor(modelItem => item.petowner)
</td>
<td>
@Html.DisplayFor(modelItem => item.ostreet)
</td>
</tr>
}
</table>
@Html.PagedListPager(Model, page=>Url.Action("ownerfind","owner", new{page, search=Request.QueryString["search"]}), new PagedListRenderOptions(){Display=PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation=true,MaximumPageNumbersToDisplay=5})
</div>
jQuery 段:
$("#ownertable td:nth-child(1)").click(function (event) {
event.preventDefault();
var $td = $(this).closest('tr').children('td');
var currentid = $.trim($td.eq(0).text());
alert("hi:" + currentid);
$.ajax({
url: 'owner/Getowner',
type: 'POST',
data: 'nownerid=' + currentid,
dataType: "json",
success: function (result) {
alert("hello");
//alert(result.petowner);
opener.$('input#ownerid').val(result.ownerid);
opener.$('#petowner').val(result.petowner);
opener.$('input#ostreet').val(result.ostreet);
self.close();
}
});
});
问题来了: 如果我使用第一个控制器操作 public ActionResult Index(string search, int? page),那么我会得到预期的 json 结果。但是,如果我使用第二个控制器动作 public ActionResult Ownerfind(string search, int? page) 然后我没有得到 json 结果。 两个控制器操作都在屏幕上工作,我得到了 alert("hi:" + currentid); 但是,如果我单击索引视图中使用的表,我只会得到一个 json 结果。 为什么 ownerfind 视图不能与 jquery 一起使用?它正确显示没有 json 结果。 我想要单独的视图,因为一个是查找,另一个是添加和编辑的常规。
【问题讨论】:
标签: asp.net-mvc jquery