基本上我试图通过 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"));
}
}
}
我希望在当前页面上插入部分视图 - 而不是重定向到它。