简体   繁体   English

MVC4中的Ajax形式

[英]Ajax form in MVC4

I'm code web MVC4 by Visual 2012. I code form show list Student. 我使用Visual 2012对Web MVC4进行编码。 Now, i want to delete a row in list by ajax but not working. 现在,我想通过ajax删除列表中的一行,但不起作用。 How do me to call ajax and return view. 我如何调用ajax并返回视图。 Below is my code: 下面是我的代码:

This is Controller: 这是控制器:

[HttpGet]
public ActionResult AjaxPage()
{
    demoMVC4Entities db = new demoMVC4Entities();
    var _listProvince = db.T_Provinces;
    ViewBag.ddl_Province = new SelectList(_listProvince, "Province_ID", "Province_Name");

    var _listStudent = db.T_Student;

    return View(_listStudent);
}
[HttpPost]
public ActionResult del(FormCollection fc, int delid)
{
    demoMVC4Entities db = new demoMVC4Entities();
    T_Student _delStudent = db.T_Student.Single(n => n.MA == delid);

    db.T_Student.DeleteObject(_delStudent);
    db.SaveChanges();

    var _listStudent = db.T_Student;
    return View(_listStudent);
}

This is View: 这是视图:

<div id="result">
    <% using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId="result" }))
       { %>
    <div class="n_control">
        <div>Keyword: </div><% string _ns = ViewBag.Keyword; %>
        <div><%= Html.TextBox("Keyword", _ns)%></div>
        <div>Province: </div>
        <div><%= Html.DropDownList("ddl_Province", "--- Tất cả ---")%></div>
        <div><input type="submit" value="Search" /></div>
    </div>
    <%} %>
    <div class="n_result">
        <table>
            <tr>
                <th></th>
                <th>Ma</th>
                <th>ID</th>
                <th>Name</th>
                <th>Phone</th>
                <th>Email</th>
                <th>Province</th>
                <th></th>
            </tr>
            <%foreach(var item in Model){ %>
            <tr>
                <td><input type="checkbox" name="nam" id="nam" value="<%= item.MA %>" /></td>
                <td><%= Html.DisplayFor(n=>item.MA) %></td>
                <td><%= Html.DisplayFor(n=>item.ID) %></td>
                <td><%= Html.DisplayFor(n=>item.Name) %></td>
                <td><%= Html.DisplayFor(n=>item.Phone) %></td>
                <td><%= Html.DisplayFor(n=>item.Email) %></td>
                <td><%= Html.DisplayFor(n=>item.Province) %></td>
                <td><a href="#" class="del_i" v="<%= item.MA %>">Del</a></td>
            </tr>
            <%} %>
        </table>
    </div>
</div>
<script>
    $(function () {
        $('.del_i').click(function () {
            var _val = $(this).attr("v");
            $.ajax({
                url: '<%= Url.Action("del","Home")%>',
                type: 'POST',
                data: { fc: $(this).serialize(), delid: _val },
                success: function (result) {
                    $('#result').html(result);
                }
            });
        });
    });
</script>

Please help me... Thanks.! 请帮助我...谢谢。

You should store data using the data-* attributes Change the link to 您应该使用data-*属性存储数据将链接更改为

<a href="#" class="del_i" data-id="<%= item.MA %>">Del</a>

and access the value with 并使用访问值

var _val = $(this).data("id");

$(this).serialize() is trying to serialize the link which is pointless and your not using it anyway so it should be just $(this).serialize()试图序列化没有意义的链接,并且您无论如何都不使用它,因此它应该只是

data: { delid: _val },

then change the POST method to 然后将POST方法更改为

public ActionResult del(int delid)

There is no need to return a view. 无需返回视图。 You can just return a json result indicating success, and then remove the associated row from the view. 您可以只返回表示成功的json结果,然后从视图中删除关联的行。

The full code can be 完整的代码可以是

Script 脚本

$('.del_i').click(function () {
  var _val = $(this).data("id");
  var row = $(this).closest('tr');
  $.ajax({
    url: '<%= Url.Action("del","Home")%>',
    type: 'POST',
    dataType: 'json',
    data: { delid: _val },
    success: function (result) {
      if(result) {
        row.remove();
      }
    }
  });
});

Controller 控制者

[HttpPost]
public ActionResult del(int delid)
{
  demoMVC4Entities db = new demoMVC4Entities();
  T_Student _delStudent = db.T_Student.Single(n => n.MA == delid);
  db.T_Student.DeleteObject(_delStudent);
  db.SaveChanges();
  return Json(true); // or return Json(null) if there was an error
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM