2015-03-03 167 views
1

在這裏遇到問題...當我提交我的Ajax部分表單(在索引頁面上)時,我得到_IndexPartial頁面作爲新頁面返回(當然沒有佈局)。C#部分視圖返回新頁面

下面是索引頁面的代碼:

@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行改爲this(below),而不是一切正常:

return PartialView(prod); 

我的問題:改變了什麼?我曾經能夠返回PartialView(ViewName, Model),但現在只有當我返回PartialView(Model)時才起作用。這是爲什麼發生?

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

+0

你正在使用哪個'Ajax.BeginForm()'重載?這看起來不正確。 https://msdn.microsoft.com/en-us/library/system.web.mvc.ajax.ajaxextensions.beginform(v=vs.118).aspx – ataravati 2015-03-03 01:03:26

+0

@ataravati我剛剛改變了'Ajax.BeginForm( )'對另一個超載我試過了,這給了我同樣的東西......一個新的頁面。 – Targaryen 2015-03-03 01:05:26

回答

0

我剛纔知道我改變了什麼。我已經停止在我的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"));