如何在ajax表单提交后获取Action返回的数据

时间:2014-11-13 12:45:49

标签: jquery asp.net ajax asp.net-mvc json

我的控制器中有PartialView方法HttpGet。并且httpPost我正在返回JsonResult。我提交了ajax表单但在表单提交后无法获取Json数据。 这是我的控制器代码

        [HttpGet]
        public PartialViewResult EditStudent(int Id)
        {
            var getStudent = _db.Student.Find(Id);
            if (getStudent != null)
            {
                return PartialView("EditStudent", getStudent);
            }
            return PartialView("EditStudent");
        }


        [HttpPost]
        public JsonResult EditStudent(Student st)
        {
            var getData = _db.Student.Find(st.Id);
            getData.Name = st.Name;
            getData.Class = st.Class;
            _db.SaveChanges();
            IList<Student> getSt = _db.Student.ToList();
            return Json(getSt, JsonRequestBehavior.AllowGet);
        } 

这是我的partialView页面代码

  @using (Ajax.BeginForm("EditStudent", "Home", new AjaxOptions { UpdateTargetId = "dv", InsertionMode = InsertionMode.Replace, HttpMethod = "POST", OnSuccess = "**BindSt**()" }))
        {
            <div class="modal-body">
                @if (Model != null)
                {
                    <table>
                        <tr>
                            <td>
                            @Html.HiddenFor(m => m.Id)
                                Student Name
                            </td>
                            <td>@Html.TextBoxFor(m => m.Name, new { id = "txtStName" })
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Student Class
                            </td>
                            <td>@Html.TextBoxFor(m => m.Class, new { id = "txtStClass" })
                            </td>
                        </tr>
                    </table>
                }
                else
                {
                    <span>@Html.LabelFor(m => m.Id) is Invalid Id</span>
                }
            </div>

        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">
                Close</button>
                <input type="submit" class "btn btn-primary" value="Submit" />
        </div>
        }

如你所见,我想调用我的js中写的 BindSt()函数。

这是我的绑定功能

function BindSt(data) {
            var tblHtml = '';
            $.each(data, function (i, d) {
                tblHtml += "<tr><td>" + d.Id + "</td><td>" + d.Name + "</td><td>" + d.Class + "</td><td><img src='../../Images/edit.jpg' style='width:25px;cursor:pointer;height:25px;' class='dvEdit'/> &nbsp; <img src='../../Images/Delete.png' style='width:25px;cursor:pointer;height:25px;' class='dvDelete' /></td></tr>";
            });
            $('#tblst').find('tbody').html(tblHtml);
        }

但我的bindst函数需要一个参数数据。我的行动 EditStudent 返回我要通过的json。我怎么能做这些数据,以便我可以传递这个功能。

0 个答案:

没有答案