在ASP.NET MVC3项目中,我有2个控制器:一个是
HomeController.cs
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
//
// POST: /Home/CheckLogin/
[HttpPost]
public ActionResult CheckLogin()
{
// setting the session variable if login is correct
// and redirecting to /ReWeb/
// else, reloading the login.
}
}
另一个是 ReWebController.cs
public class ReWebController : Controller
{
//
// GET: /ReWeb/
public ActionResult Index()
{
// Session verification
if (Session["_ReWeb"] != null)
{
return View();
}
// If session is null or not valid,
// redirect to login page
return RedirectToAction("Index", "Home");
}
}
在登录页面,即 HomeController 索引操作返回的视图中,我有以下表格:
<div id="loginPanel">
<form enctype="application/x-www-form-urlencoded" id="login" method="post">
<h1>Log In</h1>
<fieldset id="inputs">
<input id="Utente" name="Utente" type="text" placeholder="Utente" autofocus="true" required="true" />
<input id="Password" name="Password" type="password" placeholder="Password" required="true" />
</fieldset>
<fieldset id="actions">
<input type="submit" id="submit" value="Log in" onclick="javascript:window.open('/Home/CheckLogin/', 'WebClient', 'width=500,height=250,left=100,top=100')"/>
</fieldset>
</form>
</div>
我要做的是,当用户点击登录按钮时,应用程序会在弹出页面中返回 ReWebController 的视图。从我在这里做的,它打开弹出窗口,但是给出了404错误:>应用程序中的服务器错误。无法找到该资源。请求的URL:/ Home / CheckLogin / 。
我该如何实现这种方法?我做得对吗?
非常感谢!
答案 0 :(得分:1)
您收到404错误,因为您的HomeController仅包含接受POST请求的CheckLogin操作。单击登录按钮时,只需在新窗口中加载URL即GET请求。
处理此问题的一个选项是让CheckLogin操作返回包含true或false的JSON对象,具体取决于登录是否成功。在登录视图中,您可以执行以下操作:
$('form#login').submit(function () {
var data = {}; // put the login data in here
$.post('/Home/CheckLogin', data, function (result) {
// Here you can check 'result'
if (result.success === true) {
window.open('/ReWeb', 'WebClient', 'width=500,height=250,left=100,top=100');
}
else {
// The login failed
}
}).error(function() {
// Something went wrong when trying to make the request.
// Like a 404, 501, ... error
});
return false;
});
答案 1 :(得分:0)
您只需将 action =“Checklogin”添加到表单元素即可。