【问题标题】:jquery post to asp.net mvc controllerjquery 发布到 asp.net mvc 控制器
【发布时间】: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


    【解决方案1】:

    将 [HttpPost] 属性添加到来自 Contoller 的 Getowner 操作

    并从获取所有者返回如下:

    return Json(new { Data = existingCust });
    

    【讨论】:

    • 我已经有了 [HttpPost]。如前所述,如果我从 OwnerController 加载索引页面并点击所有者,那么一切正常。 json响应和一切正常。而如果我在浏览器中加载 Ownerfind.cshtml 页面并单击所有者,则 jquery 会到达 alert("hi:" + currentid); ajax 部分不处理,Getowner 永远不会发生。我正在尝试您的建议 return Json(new { Data = existingCust });而不是 return Json(existingCust);
    • 对不起,这很尴尬。我将 jquery url:'owner/Getowner' 中的一行更改为 url:'/owner/Getowner',它可以工作。认为一个斜线就完全不同了。但是感谢您的回答,并感谢 stackoverflow 提供论坛。
    猜你喜欢
    • 2021-08-08
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多