[英]Post method that calls another one for returning the view
在MVC4项目中,我需要根据可能出现的某些消息来“刷新”页面,否则,我将重定向到页面,如果如果出现消息则再次显示该页面,我想避免只返回View作为当用户尝试刷新它时,它将导致重复提交。
我想做的是
[HttpGet]
public ActionResult SampleMethod()
{
viewModel = _builder.Build();
return View(viewModel);
}
[HttpPost]
public void SampleMethod(SampleViewModel viewModel)
{
if (ModelState.IsValid)
{
var response = serviceCall;
var errorMessages = response.ErrorMessages;
if (!errorMessages.Any())
{
//Redirect to proper view
}
else
vm = _builder.Build();
}
else vm = _builder.Build(); //There is some validation error I rebuild
CashbackOffersConfirmation(vm);
}
public ActionResult SampleMethodConfirmation(SampleViewModel viewModel)
{
return View("SampleMethod", viewModel);
}
它经历了整个过程,但最后一页是... / SampleMethod而不是... / SampleMethodConfirmation,并且为空白,
这与路由有关(在此非常丢失)吗? 这是正确的方法吗?
谢谢
为了将对象模型从视图传递到控制器,您需要发出发布请求。 确保您使用的表单将生成发布请求。 同时将SampleMethodConfirmation方法发布。
例如:在控制器的方法顶部添加[HttpPost]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.