JQuery Ajax在MVC3中不起作用

[英]JQuery Ajax doesn't work in MVC3


In User management, there's 4 pages: List, Create, Details and Edit. All 4 pages render the partial ListPartial. Here's the Ajax script I use to load the ListPartial:

在用戶管理中,共有4個頁面:列表,創建,詳細信息和編輯。所有4個頁面都呈現部分ListPartial。這是我用來加載ListPartial的Ajax腳本:

<script type="text/javascript">
    $(function () {
        $('.leftContainer').load('ListPartial');
    });
</script>

The List and Create work correctly but Details and Edit don't. And I've found the way to make it work by using the url "User/Details?id=1" instead of the normal "User/Details/1". How can I make it work correctly with the normal url?

列表和創建工作正常但詳細信息和編輯沒有。我已經找到了通過使用URL“User / Details?id = 1”而不是正常的“User / Details / 1”來使其工作的方法。如何使其與普通網址一起正常工作?

Edited:

ListPartial.cshtml (Where the link direct to user details page and 4 views List, Create, Details and Edit use Ajax to load it)

ListPartial.cshtml(其中鏈接指向用戶詳細信息頁面和4個視圖List,Create,Details和Edit使用Ajax加載它)

@model IEnumerable<ARS.Models.User>
<div class="leftToolbar">
@using (Html.BeginForm("Create", "User"))
{
    <button name="button" class="btnAdd" value="Create"></button>
    <input id="Text1" type="text" class="txtSearch" />
}
</div>
<div class="smallList">
    <table>
        <tr>
            <th>
                Email đăng nhập
            </th>
            <th>
                Nhóm
            </th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.ActionLink(Html.DisplayFor(modelItem => item.Email).ToString(), "Details", new { id=item.ID })
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Group.Name)
                </td>
            </tr>
        }
    </table>
</div>

UserController/Details

public ViewResult Details(int id)
{
    User user = db.Users.Find(id);
    return View(user);
}

Global.asax

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
    );
}

1 个解决方案

#1


0  

In Controller

// GET: /User/Edit/5
public ActionResult Edit(int id)
        {
           //code..
        }

// GET: /User/Details/5
public ViewResult Details(int id)
        {
           //code..
        }

In cshtml page

在cshtml頁面中

@Html.ActionLink("Edit", "Edit", new { id=item.UserId}) |
@Html.ActionLink("Details", "Details", new { id = item.UserId })

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/07/21/72fe26e7150c309518170e32a574a21.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com