我的@Ajax.BeginForm模型有一个布尔值(@Html.CheckBoxFor).如果选中此选项,我希望我的HttpPost操作重定向到新页面.否则我希望它继续作为@Ajax.BeginForm并更新页面的一部分.
这是我的HttpPost动作(注意:Checkout是我模型中的布尔值)
控制器:
[HttpPost]
public ActionResult UpdateModel(BasketModel model)
{
if (model.Checkout)
{
// I want it to redirect to a new page
return RedirectToAction("Checkout");
}
else
{
return PartialView("_Updated");
}
}
Run Code Online (Sandbox Code Playgroud) 我将jQuery AJAX调用的登录信息发送到MVC 4控制器:
$.post(url, data, function (response) {
if (response=='InvalidLogin') {
//show invalid login
}
else if (response == 'Error') {
//show error
}
else {
//redirecting to main page from here for the time being.
window.location.replace("http://localhost:1378/Dashboard/Index");
}
});
Run Code Online (Sandbox Code Playgroud)
如果登录成功,我想根据用户类型将用户从服务器端重定向到适当的页面.如果登录失败,则会将一个字符串发送回用户:
[HttpPost]
public ActionResult Index(LoginModel loginData)
{
if (login fails)
{
return Json("InvalidLogin", JsonRequestBehavior.AllowGet);
}
else
{
// I want to redirect to another controller, view, or action depending
// on user type.
}
}
Run Code Online (Sandbox Code Playgroud)
但有问题:
如果此方法返回'ActionResult',那么我收到错误not all code paths …
Ajax.BeginForm调用一个动作,然后返回JSON.如何在OnCompletejs函数中访问JSON对象?
所以我的Ajax.BeginForm样子......
using (Ajax.BeginForm("Coupon", new AjaxOptions { OnSuccess = "CouponSubmitted" }))
Run Code Online (Sandbox Code Playgroud)
我的OnSuccess功能看起来像这样......
function CouponSubmitted() {
var data = response.get_response().get_object();
alert(data.success);
}
Run Code Online (Sandbox Code Playgroud)
我也试过......
function CouponSubmitted(data) {
alert(data.success);
}
Run Code Online (Sandbox Code Playgroud)
我的控制器"优惠券"返回此...
return Json(new { success = false, nameError = nameError, emailError = emailError });
Run Code Online (Sandbox Code Playgroud)
关于如何访问返回的Json的任何想法?
我有以下视图,其中包含一个Ajax.BeginForm: -
@using (Ajax.BeginForm("ChangeDevicesSwitch", "Switch", new AjaxOptions
{
InsertionMode = InsertionMode.InsertBefore,
UpdateTargetId = "result",
LoadingElementId = "progress2",
HttpMethod= "POST"
,
OnSuccess = "createsuccess",
OnFailure = "createfail"
}))
//code goes here
<p><img src="~/Content/Ajax-loader-bar.gif" class="loadingimage" id="progress2" /></p>
<div id ="result"></div>
Run Code Online (Sandbox Code Playgroud)
以及将从Ajax.Bginform调用的以下Action方法: -
public ActionResult ChangeDevicesSwitch(SwitchJoin s)
{//code goes here
try
{
var count = repository.changeDeviceSwitch(s.Switch.SwitchID, (Int32)s.GeneralSwitchTo, User.Identity.Name.Substring(User.Identity.Name.IndexOf("\\") + 1));
repository.Save();
return RedirectToAction("Details", new { id = s.GeneralSwitchTo });
}
catch (Exception e)
{
return Json(new { IsSuccess = "custome", description = "Error …Run Code Online (Sandbox Code Playgroud) 所以我在这里看到了这个问题,但它并没有真正为我解决这个问题.
我在我的开发者电脑上的IIS5上运行了一个ASP.NET MVC 3 + Razor应用程序,然后在我的开发Web服务器上安装了IIS6.在部署之前,一切都很顺利.我正在bin部署所有东西,在这方面没有问题(我可以告诉).
现在我得到这个Child动作不允许在我的页面上执行重定向动作错误.我不确定如何确定它失败的地方.
我正在使用@ Html.Action来获取一些包含数据的下拉框:
public ActionResult Hierarchy()
{
List<Store> storeList = DBService.getStores();
if (DBService.Error != null)
return RedirectToError(DBService.Error);
List<Department> deptList = DBService.getDepts();
if (DBService.Error != null)
return RedirectToError(DBService.Error);
VM_Hierarchy hierarchy = new VM_Hierarchy(storeList, deptList);
return View(hierarchy);
}
Run Code Online (Sandbox Code Playgroud)
如果我删除@Html.Action行,页面将呈现.如果我对控制器动作执行AJAX请求,它将会中断,例如:
[HttpPost]
public ActionResult InventoryList(string fromDate, string toDate)
{
List<Inventory> inventories = DBService.getInventories(fromDate, toDate);
if (DBService.Error != null)
return RedirectToAction("Error");
return View(inventories);
}
Run Code Online (Sandbox Code Playgroud)
如果这不正确,我应该如何重定向到错误页面或指示在帖子上返回什么视图?任何帮助表示赞赏.谢谢.