簡體   English   中英

C#PartialView返回新頁面

[英]C# PartialView returns new page

在這里遇到問題...當我提交Ajax部分表單(在“索引”頁面上)時,我將_IndexPartial頁面作為新頁面返回(當然,沒有布局)。

這是“索引”頁面的代碼:

@model List<s84.Domain.s84_CustomerProduct>

@{
    ViewBag.Title = "Customer/Product Order";
    Layout = "~/Views/Shared/_NewLayout.cshtml";
}

@using (Ajax.BeginForm("Index", "CustomerProduct", new AjaxOptions { UpdateTargetId = "data-tbl", HttpMethod = "Post" }))
{
    <div class="search">
        @Html.DropDownList("CustomerID",
            new SelectList(s84.Domain.s84_Customer.listItems(), "CustomerID", "CustomerName"))
        <input type="submit" value="Search" />
    </div><br/><br/>
    <div id="data-tbl">
        @Html.Partial("_IndexPartial")
    </div>
}

這是_IndexPartial頁面的代碼:

@model List<s84.Domain.s84_CustomerProduct>

<table class="table">
    <thead>
        <tr>
            <td>&nbsp;</td>
            <td>Product</td>
            <td>Order</td>
            <td>Required Days</td>
        </tr>
    </thead>
    <tbody>
        @for (int i = 0; i < Model.Count; i++)
        {
            <tr>
                <td>
                        @Html.ActionLink("Edit", "Edit", new { id = Model[i].CustomerProductID }, null)
                        <text>/</text>
                        @Html.ActionLink("Del", "Delete", new { id = Model[i].CustomerProductID }, null)
                </td>
                <td>@Model[i].s84_Product.ProductName</td>
                <td>@Model[i].ProductOrder</td>
                <td>@Model[i].RequiredDays</td>
            </tr>
        }
    </tbody>
</table>

這是控制器代碼:

    [HttpGet]
    public ActionResult Index()
    {
        List<s84_CustomerProduct> lst = s84_CustomerProduct.listItemsByCustomer();

        return View(lst);
    }

    [HttpPost]
    public ActionResult Index(int CustomerID)
    {
        List<s84_CustomerProduct> prod = s84_CustomerProduct.listItemsByCustomer(CustomerID);

        return PartialView("_IndexPartial", prod);
    }

如果我將Controller Post方法中的return PartialView行更改為此(如下),則一切正常:

return PartialView(prod);

我的問題:發生了什么變化? 我曾經能夠返回PartialView(ViewName, Model)但是現在它僅在我返回PartialView(Model)時才起作用。 為什么會這樣呢?

編輯:我剛意識到我也得到一個查詢字符串,當Post調用返回PartialView時。 每次發布表單時,都會重定向到localhost/CustomerProduct?Length=15 無論我從下拉菜單中選擇哪個客戶, Length=15始終存在。

我只是想出了我所做的更改。 我已經停止在我的jQuery包中包含jQuery驗證腳本文件。 我將這些JS文件重新添加到我的捆綁包中...

  • jquery.validate.js
  • jquery.unobtrusive-ajax.js
  • jquery.validate.unobtrusive.js

顯然,Razor或MVC的某些部分需要這些文件。 這是解決問題的代碼:

bundles.Add(new ScriptBundle("~/Content/jquery").Include(
    "~/Scripts/jquery-2.0.3.js",
    "~/Scripts/jquery.validate.js",
    "~/Scripts/jquery.unobtrusive-ajax.js",
    "~/Scripts/jquery.validate.unobtrusive.js"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM