在RedirectToAction控制器内调用时,它会使用HTTP GET自动重定向.如何明确告诉它使用HTTP POST?
我有一个接受GET和POST请求的动作,我希望能够RedirectToAction使用POST并发送一些值.
像这样:
this.RedirectToAction(
"actionname",
new RouteValueDictionary(new { someValue = 2, anotherValue = "text" })
);
Run Code Online (Sandbox Code Playgroud)
我希望使用HTTP POST而不是GET发送someValue和anotherValue值.有谁知道如何做到这一点?
使用ASP.NET MVC可能需要一些情况(例如表单提交)RedirectToAction.
其中一种情况是,您在表单提交后遇到验证错误并需要重定向回表单,但希望URL反映表单的URL,而不是它提交的操作页面.
由于我要求表单包含最初POST编辑的数据,为了方便用户和验证目的,我如何通过RedirectToAction()?如果我使用viewData参数,我的POST参数将更改为GET参数.
如果我的ModelState中存在错误而不丢失我的ModelState信息,如何返回不同操作的结果或将用户移动到其他操作?
情景是; 删除操作接受由我的索引操作/视图呈现的DELETE表单中的POST.如果删除中出现错误,我想将用户移回索引操作/视图,并显示删除操作存储的错误ViewData.ModelState.如何在ASP.NET MVC中完成?
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Delete)]
public ActionResult Delete([ModelBinder(typeof(RdfUriBinder))] RdfUri graphUri)
{
if (!ModelState.IsValid)
return Index(); //this needs to be replaced with something that works :)
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud) 我正在使用ASP.NET MVC 4.我试图将数据从一个控制器传递到另一个控制器.我没有做到这一点.我不确定这是否可行?
这是我的源操作方法,我想从中传递数据:
public class ServerController : Controller
{
[HttpPost]
public ActionResult ApplicationPoolsUpdate(ServiceViewModel viewModel)
{
XDocument updatedResultsDocument = myService.UpdateApplicationPools();
// Redirect to ApplicationPool controller and pass
// updatedResultsDocument to be used in UpdateConfirmation action method
}
}
Run Code Online (Sandbox Code Playgroud)
我需要将它传递给此控制器中的此操作方法:
public class ApplicationPoolController : Controller
{
public ActionResult UpdateConfirmation(XDocument xDocument)
{
// Will add implementation code
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
我在ApplicationPoolsUpdate动作方法中尝试了以下操作,但它不起作用:
return RedirectToAction("UpdateConfirmation", "ApplicationPool", new { xDocument = updatedResultsDocument });
return RedirectToAction("UpdateConfirmation", new { controller = "ApplicationPool", …Run Code Online (Sandbox Code Playgroud) 是否可以做一个职位从行动"保存"在控制器"产品"到行动"白水"在控制器"类别"?
并且还传递FormCollection作为参数
嗨,
我有一个看起来像这样的动作:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Register(AdRegister adRegister, IEnumerable<HttpPostedFileBase> files)
Run Code Online (Sandbox Code Playgroud)
AdRegister是一个复杂的类,我需要将其传递给Register操作中的一个重定向方法,如下所示:
return this.RedirectToAction("Validate", adRegister);
Run Code Online (Sandbox Code Playgroud)
Validate操作如下所示:
public ActionResult Validate(AdRegister adRegister)
Run Code Online (Sandbox Code Playgroud)
我知道我可以传递简单的参数但在这种情况下它是一个复杂的对象.此示例不起作用,adRegister的属性将为null.
这是可能的,如果是这样,怎么样?
最好的祝福
更多信息:注册操作将采用adRegister并对其执行魔术,然后将其发送到Validate操作.Validate操作将向用户返回验证页面.当用户点击授权按钮时,adRgister将从表单中填充,然后发送到vValidate帖子,在那里保存.我已经查看过将adRegister暂时放在缓存或数据库中,但如果我可以简单地将它传递给下一个操作,那就更好了.
我正在尝试将一个复杂的对象(可以序列化,如果有帮助)传递给另一个视图.
目前这是我的代码,在某些控制器方法中: -
User user = New User { Name = "Fred, Email = "xxxx" };
return RedirectToAction("Foo", user);
Run Code Online (Sandbox Code Playgroud)
现在,我在同一个控制器中有以下动作......
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Foo(User user)
{
...
}
Run Code Online (Sandbox Code Playgroud)
当我在那里设置一个断点,代码中并停在那里,但价值user是null.我需要做什么?我错过了什么global.asax吗?
欢呼:)
我如何使用redirectAction在动作之间发送数据?
我正在使用PRG模式.我想做出类似的东西
[HttpGet]
[ActionName("Success")]
public ActionResult Success(PersonalDataViewModel model)
{
//model ko
if (model == null)
return RedirectToAction("Index", "Account");
//model OK
return View(model);
}
[HttpPost]
[ExportModelStateToTempData]
[ActionName("Success")]
public ActionResult SuccessProcess(PersonalDataViewModel model)
{
if (!ModelState.IsValid)
{
ModelState.AddModelError("", "Error");
return RedirectToAction("Index", "Account");
}
//model OK
return RedirectToAction("Success", new PersonalDataViewModel() { BadgeData = this.GetBadgeData });
}
Run Code Online (Sandbox Code Playgroud)