简体   繁体   English

使用AJAX在jQuery对话框内的jQuery选项卡内加载MVC用户控件(C#内部)

[英]Using AJAX to load an MVC user control inside of a jquery tab inside of a jquery dialog (C# Inside)

I have an index page for listing products. 我有一个列出产品的索引页。 From this page I would like to be able to open a dialog with the following tabs. 从此页面,我希望能够打开一个包含以下标签的对话框。 Edit/Create Product, Product Images, and a tab for Brands. 编辑/创建产品,产品图片和“品牌”标签。 The brands tab isn't specific to the product being edited/created, and doesn't require being passed an ID. 品牌标签不特定于要编辑/创建的产品,也不需要传递ID。 I have everything broken up into the following partial views: NewProduct, EditProduct, ProductImages, and Brands. 我将所有内容分解为以下部分视图:NewProduct,EditProduct,ProductImages和Brands。 My current implementation uses Jquery dialogs and tabs, but I need help getting the correct behavior. 我当前的实现使用Jquery对话框和选项卡,但是我需要获得正确行为的帮助。

Currently - I use an Ajax.ActionLink to call NewProductDialog, which prepares a viewmodel with a boolean ProductEditMode set to false and returns it along with the partial view. 当前-我使用Ajax.ActionLink调用NewProductDialog,它准备一个布尔值ProductEditMode设置为false的视图模型,并将其与部分视图一起返回。 The Ajax request takes the returned view and populates a with the ID "ProductDialog". Ajax请求采用返回的视图,并用ID“ ProductDialog”填充ID。 However, the partial view thats loading contains the javascript for initializing the dialog and tabs, and doesn't appear to be working. 但是,正在加载的局部视图包含用于初始化对话框和选项卡的javascript,并且似乎无法正常工作。 I'm at a loss here, and perhaps I'm doing this horribly wrong, so I figured I would ask here first. 我在这里茫然不知所措,也许我在做这个可怕的错误,所以我想我会先在这里问。 This is my first real attempt at using AJAX. 这是我第一次真正尝试使用AJAX。

Complete productDialog.ascx 完整的productDialog.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MHNHub.Areas.Admin.ViewModels.ProductViewModel>" %>

    <div id="dialog" title="Edit Product">
        <div id="tabContainer">
            <ul>
                <% if (Model.ProductEditMode) {%>
                <li><%:Html.ActionLink("Edit Product", "EditProduct", "Product", new { id = Model.Product.Id }, null)%></li>
                <li><%:Html.ActionLink("Product Images", "Images", "Product", new { id = Model.Product.Id }, null)%></li>
                <% } else { %>
                <li><%:Html.ActionLink("New Product", "NewProduct", "Product")%></li>
                <%} %>
                <li><%:Html.ActionLink("Brands", "Brands", "Product")%></li>
            </ul>

        </div>

    </div>

    <script type="text/javascript">
    $(function () {
        $("#dialog").dialog({
            bgiframe: false,
            height: 600,
            width: 900,
            padding: 0,
            modal: true,
            autoOpen: true,
            resizable: true
        }),

        $("#tabContainer").tabs()

    });

</script>

Complete Index.aspx 完整的Index.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MHNHub.Areas.Admin.ViewModels.ProductViewModel>" %>

<%@ Import Namespace="MHNHub.Helpers" %>
<%@ Import Namespace="MHNHub.Models" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeaderContent" runat="server">
    <strong>Product</strong> Management
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <script type="text/javascript">
        $(document).ready(function () {
            $("#products").dataTable();
        });
    </script>


<div id="productDialog">

</div>

    <h2>Products</h2>

    <%:Ajax.ActionLink("Create New Product", "NewProductDialog", "Product", null, new AjaxOptions { UpdateTargetId = "productDialog", InsertionMode = InsertionMode.Replace })%>
    <br />
    <hr />
    <table id="products" class="display" cellpadding="0" cellspacing="0" border="0" style="width: 900px;">
        <thead>
            <tr>
                <th>
                </th>
                <th>
                    Product Description
                </th>
                <th>
                    MSRP
                </th>
                <th>
                    Is Active
                </th>
                <th>
                    Price A
                </th>
                <th>
                    Price B
                </th>
                <th>
                    Price C
                </th>
            </tr>
        </thead>
        <tbody>
            <%foreach (var item in Model.ProductList)
              { %>
            <tr>
                <td>
                    <%:Ajax.ActionLink(" ", "EditProductDialog", new { id = item.Id }, new AjaxOptions { UpdateTargetId = "productDialog", InsertionMode = InsertionMode.Replace }, new { Class="edit"})%>

                    <%: Html.ActionLink(" ", "DeleteProduct", new { id = item.Id }, new { Class = "delete" })%>
                </td>
                <td>
                    <%: item.Description %>
                </td>
                <td>
                    <%: String.Format("{0:C}", item.MSRP) %>
                </td>
                <td>
                    <%: item.IsActive %>
                </td>
                <td>
                    <%: String.Format("{0:C}", item.PriceA )%>
                </td>
                <td>
                    <%: String.Format("{0:C}", item.PriceB) %>
                </td>
                <td>
                    <%:String.Format("{0:C}",  item.PriceC) %>
                </td>
            </tr>
            <% } %>
        </tbody>
    </table>
    <br />
    <%: Html.ActionLink(" ", "Index", "Menu", null, new{id = "backToAdmin"}) %>
</asp:Content>

Related ProductController.cs snippets 相关ProductController.cs代码段

public ActionResult NewProductDialog()
        {
            var viewModel = new ProductViewModel()
            {
                ProductEditMode = false
            };

            return PartialView("ProductDialog", viewModel);
        }

        public ActionResult EditProductDialog(int id)
        {
            var product = _entities.Products.Where(p => p.Id == id).Single();
            var viewModel = new ProductViewModel()
                                {
                                    ProductEditMode = true,
                                    Product = product
                                };

            return PartialView("ProductDialog", viewModel);
        }


        public ActionResult NewProduct()
        {
            var productCategories = _entities.ProductCategories.Where(p => p.ParentId != 0).OrderBy(p => p.CategoryName).ToList();
            var brands = _entities.Brands.ToList();

            var viewModel = new ProductViewModel()
                        {
                            Product = new Product(),
                            ProductCategories = productCategories,
                            Brands = brands
                        };

            return PartialView("NewProduct", viewModel);
        }

This solved my issue: 这解决了我的问题:

missing ) after argument list error when using Ajax.ActionLink mvc2 使用Ajax.ActionLink mvc2时在参数列表错误后缺少)

Cant use Microsoft Ajax and Jquery at the same time, apparently(even though they come packaged together with mvc2) I had to rewrite my ajax calls with Jquery, and now the dialogs work fine, I've got them everywhere. 显然,不能同时使用Microsoft Ajax和Jquery(即使它们与mvc2打包在一起),我也不得不用Jquery重写ajax调用,现在对话框可以正常工作了,到处都是。

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

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