weixin_33737774 2015-04-15 05:53 采纳率: 0%
浏览 31

MVC4中的Ajax形式

I'm code web MVC4 by Visual 2012. I code form show list Student. Now, i want to delete a row in list by ajax but not working. How do me to call ajax and return view. 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.!

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 使用自定义的类型代替内置类型可行吗
      • ¥15 关于STM32的SPI和ENDAT接口编码器通信的问题
      • ¥15 关于#pdfbox#生成的PDF文件正常,转图片中文乱码的问题,如何解决?
      • ¥15 ADS中有关DAC控件的使用问题
      • ¥15 win11如何运行geoserver
      • ¥15 审稿意见请教-用ERNIE进行小样本分类的对比实验问题
      • ¥15 gem5搭建arm v8
      • ¥15 安卓时间管理APP,怎么做
      • ¥15 这段有什么bug 为什么总报错
      • ¥15 Intel Realsense深度相机(D415/D435)拍摄梯子获取梯子点云