2012-10-05 31 views
2

相当新的asp.net MVC我没有知识的方式来改变某些事情的工作。这里是其中的一个维护邮政网址后在asp.net MVC

  1. 控制器动作指数显示登录页面

  2. 控制器动作登录[HttpPost]取模型,并验证它

在这种情况下,验证失败,URL似乎设置为http://blah_blah/Users/Login(当请求导致404时,由于没有登录控制器上动作)

所以是创建一个登录行动只是为了解决问题,或任何其他解决方案,我有办法吗?

回答

6

你为什么不能在Login行动重命名为Index

public ActionResult Index() 
{ 
    return View(); 
} 

[HttpPost] 
public ActionResult Index(LoginModel model) 
{ 
    return View(); 
} 

可以使用ActionName属性

[HttpPost, ActionName("Index")] 
public ActionResult Login(LoginModel model) 
{ 
    return View(); 
} 
+0

如果我在视图的不同部分有多个发布请求会怎么样?他们都发布相同的模型,但有不同的实现。他们发送相同的模型,因为发生错误时,模型需要重新填充值以重新加载视图。 – CularBytes

1

你必须return View("Index");

+0

对不起,我的英语。我这样做,但网址更改为'HTTP:// blah_blah /用户/ Login',而不是通过'http:// blah_blah/User',有反正我能做到这一点后后'Login'操作方法 – Deeptechtons

+0

是什么控制器操作对应于http:// blah_blah/User? –

+1

@Deeptechtons回报视图不会更改URL,只能重定向像** **鸽子答案 – webdeveloper

0

试试这个:

return RedirectToAction("Index", "User", new { returnUrl, errorMessage, etc }); 
+0

这是否保持错误消息完整? – Deeptechtons

+2

在这种情况下,他将丢失验证信息。 –

+0

@KirillBestemyanov'TempData'将帮助他或只是创建'和'[HttpPost] Index'行动';) – webdeveloper