MVC 4异步调用操作

时间:2013-04-12 09:59:46

标签: c# jquery ajax asp.net-mvc

我正在使用MVC4和Entity Framework来开发一个wep应用程序。在我的一个观点中,我有一份所有人都有详细信息的人员名单。开头时,只显示名字和姓氏,但有一个链接可用于显示详细信息,我希望尽可能保持动态。

我的操作会返回Json(object, JsonRequestBehavior.AllowGet)

任何想法的人?

编辑:我的行动

        public ActionResult ListByOwner(long id)
    {
        var productallocations = db.ProductAllocations.Where(obj => obj.Id_Person == id).Include("Product");
        return PartialView(productallocations);
    }

我的观点:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - My ASP.NET MVC Application</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
        <meta name="viewport" content="width=device-width" />
        @Styles.Render("~/Content/css")
        @Styles.Render("~/Content/bootstrap")
        @Scripts.Render("~/bundles/modernizr")
        <script src="../../Scripts/globalize.js" type="text/javascript"></script>
        <script src="../../Scripts/globalize.culture.fr-FR.js" type="text/javascript"></script>
    </head>
    <body>
        <div class="navbar navbar-inverse navbar-fixed-top">
            <div class="navbar-inner">
            <div class="container-fluid">
                <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                </button>
                <a class="brand" href="@Url.Action("Index", "Home")">BuSI Material Manager</a>
                <div class="nav-collapse collapse">
                <p class="navbar-text pull-right">
                    Logged in as @User.Identity.Name
                </p>
                <ul class="nav">
                    <li>@Html.ActionLink("Home","Index","Home")</li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Persons <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                          <li>@Html.ActionLink("All persons list","Index","Person")</li>
                          <li>@Html.ActionLink("All allocations list", "Index", "ProductAllocation")</li>
                        </ul>
                    </li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Cards<b class="caret"></b></a>
                        <ul class="dropdown-menu">
                          <li>@Html.ActionLink("All phone cards list", "Index", "PhoneCard")</li>
                          <li>@Html.ActionLink("All fuel cards list", "Index", "VehicleFuelCard")</li>
                        </ul>
                    </li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Products <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                          <li>@Html.ActionLink("All products list", "Index", "Product")</li>
                          <li>@Html.ActionLink("Products in stock", "ProductStock", "Product")</li>
                          <li>@Html.ActionLink("Allocated products", "AllocatedProducts", "Product")</li>
                          <li class="divider"></li>
                          <li>@Html.ActionLink("Product types", "Index", "ProductType")</li>
                          <li>@Html.ActionLink("Product companies", "Index", "ProductCompany")</li>
                        </ul>
                    </li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Vehicles <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                          <li>@Html.ActionLink("All vehicles list", "Index", "Vehicle")</li>
                          <li>@Html.ActionLink("Available vehicles", "AvailableVehicles", "Vehicle")</li>
                          <li>@Html.ActionLink("Allocated vehicles", "AllocatedVehicles", "Vehicle")</li>
                          <li class="divider"></li>
                          <li>@Html.ActionLink("Vehicle types", "Index", "VehicleType")</li>
                          <li>@Html.ActionLink("Vehicle motor types", "Index", "VehicleMotorType")</li>
                          <li class="divider"></li>
                          <li><a href="@Url.Action("Index", "VehicleInsuranceContract")">Insurance contracts</a></li>
                          <li><a href="@Url.Action("Index", "VehicleLeasingContract")">Leasing contracts</a></li>
                        </ul>
                    </li>
                </ul>
                </div>
            </div>
            </div>
        </div>

        <div id="body">
            @RenderSection("featured", required: false)
            @RenderBody()
        </div>

        <footer>
            <p>Developed by me.</p>
        </footer>

        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/jqueryval")
        @Scripts.Render("~/bundles/bootstrap")
        @RenderSection("scripts", required: false)
        <script src="../../Scripts/Utils.js" type ="text/javascript"></script>
    </body>

</html>

捆绑:

public class BundleConfig
    {
        // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                        "~/Scripts/jquery-ui-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

            bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                        "~/Content/themes/base/jquery.ui.core.css",
                        "~/Content/themes/base/jquery.ui.resizable.css",
                        "~/Content/themes/base/jquery.ui.selectable.css",
                        "~/Content/themes/base/jquery.ui.accordion.css",
                        "~/Content/themes/base/jquery.ui.autocomplete.css",
                        "~/Content/themes/base/jquery.ui.button.css",
                        "~/Content/themes/base/jquery.ui.dialog.css",
                        "~/Content/themes/base/jquery.ui.slider.css",
                        "~/Content/themes/base/jquery.ui.tabs.css",
                        "~/Content/themes/base/jquery.ui.datepicker.css",
                        "~/Content/themes/base/jquery.ui.progressbar.css",
                        "~/Content/themes/base/jquery.ui.theme.css"));
        }
    }

1 个答案:

答案 0 :(得分:0)

  

任何想法的人?

是的,您可以使用AJAX链接:

@Ajax.ActionLink(
    "Details", 
    "Details", 
    new { id = person.Id }, 
    new AjaxOptions { UpdateTargetId = "details" }
)

然后有一个占位符,其中将显示详细信息:

<div id="details"></div>

最后让你的控制器操作返回部分视图而不是JSON:

public ActionResult Details(int id)
{
    var model = repository.Get(id);
    return PartialView(model);
}

在相应的局部视图中,您可以获得详细信息:

@model Person
<div>
    @Html.DisplayFor(x => x.FirstName)
</div>
<div>
    @Html.DisplayFor(x => x.LastName)
</div>
<div>
    @Html.DisplayFor(x => x.Age)
</div>
...

备注:如果您希望jquery.unobtrusive-ajax.js正常工作,请不要忘记在您的_Layout中添加Ajax.ActionLink脚本。

相关问题