2013-06-28 28 views
0

基本上我試圖通過AJAX請求返回一個局部視圖。然而,問題在於頁面被重定向到部分視圖,而不是替換當前頁面上的內容。在當前頁面插入局部視圖而不是重定向

此處,我發送Ajax請求的觀點:

@model MediaProfits.Models.DomainEntities.ProtectedPassword 

@Scripts.Render("~/Scripts/CustomScripts/LeadChecker.js") 
@{ 
    ViewBag.Title = "Unlock " + @Model.Name; 
} 

<h2>Unlock @Model.Name</h2> 

<button onclick="StartCheckingLead('@Model.SubId');">Start Checking</button> 

@using (Ajax.BeginForm("Lead", "Check", 
    new AjaxOptions() 
    { 
     HttpMethod = "get", 
     InsertionMode = InsertionMode.Replace, 
     UpdateTargetId = "password" 
    })) 
{ 
    @Html.HiddenFor(m => m.SubId) 
    <input type="submit" value="Check For Completion" /> 
} 

@Html.Partial("_Password", "") 

這裏是返回的局部視圖控制器動作:

public PartialViewResult Lead(string subId) 
    { 
     var lead = db.Leads.Where(l => l.SubId == subId); 
     if (lead.ToList().Count > 0) 
     { 
      return PartialView("~/Views/Passwords/_Password.cshtml", "unlocked..."); 
     } 
     else 
     { 
      return PartialView("~/Views/Passwords/_Password.cshtml", ""); 
     } 
    } 

這裏是局部視圖(該頁面現在得到的重定向到):

@model System.String 
<div id="password"> 
    <label>Password:</label> 
    <label>@Model</label> 
</div> 

最後這裏是我的BundleConfig.cs文件:

using System.Web; 
using System.Web.Optimization; 

namespace MediaProfits 
{ 
    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

確保你已經包括了jquery.unobtrusive-ajax.js腳本到你的頁面(之後的jQuery):

<script type="text/javascript" src="@Url.Content("~/scripts/jquery.unobtrusive-ajax.js")"></script> 

而且,由於使用的是包,你可以只包括在視圖中的腳本部分~/bundles/jqueryval包:

@section scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

顯然,這假設你有你的佈局定義這個scripts部分:

... 
    @Scripts.Render("~/bundles/jquery") 
    @RenderSection("scripts", required: false) 
</body> 
+0

我試過在視圖中插入它,但它沒有幫助。 我沒有在我的bundleconfig中包含任何其他腳本,所以我更新了我使用的所有腳本和整個視圖的問題。我應該在哪裏插入它? –

+0

我在你的代碼中看不到你在哪裏包括這個腳本。既然你在使用bundle,你應該在你的視圖中做什麼:你應該覆蓋'scripts'部分(通常在你的佈局中定義)幷包含'〜/ bundles/jqueryval'包。我已經更新了我的答案以說明這一點。 –

+0

啊,我把它包含在佈局文件中,它現在可以工作。 非常感謝 –

相關問題