【问题标题】:Ajax request using querystring使用查询字符串的 Ajax 请求
【发布时间】:2014-02-06 19:16:35
【问题描述】:

" 我有一个控制器,它有下拉列表,并且在更改 DDL 时再次绑定调用索引视图并返回将内容加载为的部分视图: 索引控制器:

public ActionResult Index(ProductEntity Prod, string CategoryId)
{
ViewBag.Category = new SelectList(ObjStore.categories, "CategoryId", "CategoryName");
if (Request.IsAjaxRequest())
{
var model = ObjStore.products.Where(x => x.CategoryId == Prod.CategoryId);
return PartialView("_ProductMaster", model);
}
else if (CategoryId != "" && CategoryId !=null)
{
var model = ObjStore.products.Where(x => x.CategoryId == Prod.CategoryId);
return PartialView("_ProductMaster", model);
}
return View();
}

部分视图:

    @model IEnumerable<StoreManagement.Models.ProductEntity>

    <div id="Products">

    @if (Model != null)
    {    

    <table style="width:100%;">
        <tr>
            <th>Product Name </th>
            <th>Max Qty</th>
            <th>Min Qty</th>
            <th>Edit</th>
        </tr> 

        @foreach (var item in Model)
        {
            <tr>
                <td>@item.ProductName.ToString()</td>
                <td>@item.MaxQty</td>
                <td>@item.MinQty</td>
                <td>@Html.ActionLink("Edit","EditProduct",new {ProductId=@item.ProductId},null)</td>
            </tr>        
        } 

    </table>
    }
    </div>

在上面我有编辑操作链接,它重定向到编辑视图,在这个页面上,我有返回操作链接,它重定向到索引控制器,查询字符串为 CategoryId,我在选择下拉列表时使用它。

现在我想用 CategoryId 作为查询字符串来加载索引视图。

'其他代码正在为此工作'

              if (Request.IsAjaxRequest())
                {
                    var model = ObjStore.products.Where(x => x.CategoryId == Prod.CategoryId);
                    return PartialView("_ProductMaster", model);
                }
                else if (CategoryId != "" && CategoryId !=null)
                {
                    var model = ObjStore.products.Where(x => x.CategoryId == Prod.CategoryId);
                    return PartialView("_ProductMaster", model);
                }

但它只返回部分视图。我希望使用他们的 _layout 页面加载该视图。或者如果我们在 URl 中有查询字符串,是否有任何方法可以调用 DDL 的 onchange 事件?或者使 Request.IsAjaxRequest()true。"

【问题讨论】:

标签: c# jquery asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

如果你想包含布局页面,你应该返回一个 View 而不是 PartialView

            if (Request.IsAjaxRequest())
            {
                var model = ObjStore.products.Where(x => x.CategoryId == Prod.CategoryId);
                return View("_ProductMaster", model);
            }
            else if (CategoryId != "" && CategoryId !=null)
            {
                var model = ObjStore.products.Where(x => x.CategoryId == Prod.CategoryId);
                return View("_ProductMaster", model);
            }

【讨论】:

    猜你喜欢
    • 2011-10-04
    • 2013-04-06
    • 2021-07-02
    • 2020-12-02
    • 2018-08-17
    • 1970-01-01
    • 2018-08-20
    • 2016-05-04
    相关资源
    最近更新 更多