私はMVCコントローラにユーザをログインさせるメソッドを持っています。私はJson Objectを返す必要があるので、成功すると別のアクション(検索アクション)にリダイレクトする必要があります。それ以外の場合は同じページになり、ログイン失敗メッセージが表示されます。私の見解でMVCのjsonオブジェクトを含むビューを返す
[HttpPost]
[Route("login")]
public JsonResult Login(LogInRequest logInRequest)
{
LogInResponse response = new LogInResponse();
response.ReturnUrl = Url.Action ("Search", "ContreollerName");
try
{
if (ModelState.IsValid)
{
.....
if (logInRequest.UserName == "test")
{
response.IsSuccessfull = true;
}
else
{
response.IsSuccessfull = false;
response.ErrorDescription = MessageStrings.IncorrectUserName;
}
}
}
catch (Exception exception)
{
response.ErrorDescription = MessageStrings.ServerError;
response.IsSuccessfull = false;
}
return Json(response, JsonRequestBehavior.AllowGet);
}
[Route("Search")]
public ActionResult Search()
{
......
return View();
}
: これは、コントローラのコードで、私はそれではなく、ビューのJSONオブジェクトを返してみてください
@model CarFinance.Garage.Web.Models.LogInRequest
<script src="~/Scripts/Login/login.js"></script>
@using (Html.BeginForm("Login", "ContreollerName", FormMethod.Post))
{
@Html.AntiForgeryToken()
<table>
<tr>
<td><label>UserName:</label></td>
<td>
<input name="UserName" type="text" placeholder="UserName" title="UserName" class="span_12" />
</td>
</tr>
<tr>
<td><label>Password:</label></td>
<td>
<input name="Password" type="password" placeholder="Password" title="password" class="span_12" />
</td>
</tr>
<tr>
<td colspan="2"><input id="btnLogin" type="submit" value="Log In" /></td>
</tr>
</table>
}
何でも、私はjqueryのではなく、まだそれをいくつかのコードを入れてみてください私はjqueryの中でいくつかのコードを持ってしてみてくださいそれでも、それはではないだろう
{
ReturnUrl: "/Search",
IsSuccessfull: true,
ErrorDescription:
ValidationErrors: [ ]
}
:
でもjqueryのメソッドに、ちょうどこのような画面が表示されません。$("#btnLogin").click(function() {
alert("test");
.......
'login.js'には何がありますか? –
jsonオブジェクトを返すと、なぜajaxでフォームを投稿しないのですか? –
'JsonResult'を返しません。ログインが成功した場合は 'return RedirectToAction(,,,);を使用してリダイレクトし、そうでない場合は' ModelStateError'を追加してビューを返して表示します。 –