0

我在ASP.NET MVC 4初學者,我有一個問題。基本上我有這個控制器:MVC模板編輯器和帖子

public ViewResult Login() 
{ 
    return View(new LoginViewModel()); 
} 

[HttpPost] 
public ActionResult Login(LoginViewModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     if (authProvider.Authenticate(model.LoginUserName, model.LoginPassword)) 
     { 
      return Redirect(Url.Action("Index", "Home")); 
     } 
     TempData["message"] = "Nome utente e/o password errati!!!"; 
     return View(); 
    } 
    return View(); 
} 

這實現了一個簡單的登錄視圖。我還創建了一個視圖模型:

public class LoginViewModel 
{ 
    [Required(ErrorMessage = "Il nome utente è obbligatorio")] 
    [UIHint("TextBoxLogin")] 
    public string LoginUserName { get; set; } 

    [Required] 
    public string LoginPassword { get; set; } 
} 

最後,我創建了EditorTemplate:

@model string 

<input name="@ViewData.TemplateInfo.HtmlFieldPrefix" id="@ViewData.TemplateInfo.HtmlFieldPrefix"data-validation="required" data-validation-error-msg="@ViewData["HelpMessage"]" value="@Model" /> 

到目前爲止好。問題在於此。如果我把這個觀點:

@using(Html.BeginForm()) { 
    @Html.ValidationSummary(true) 
    @Html.EditorForModel() 
    <p><input type="submit" value="Log in" /></p> 
} 

它的工作原理就像一個魅力(但是卻讓很多不HTML想進入頁),事實上,當我點擊提交按鈕它去POST控制器的actionResult。如果我把這個:

@using (Html.BeginForm("Login","Account",FormMethod.Post)) 
{ 
    <p class="username"> 
     <label for="UserName">Nome utente</label> 
     @Html.EditorFor(m => m.LoginUserName, new { HelpMessage = "Il nome utente è obbligatorio!!!" }); 
    </p> 
    <p class="password"> 
     <label for="Password">Password</label> 
     @Html.EditorFor(m => m.LoginPassword) 
    </p> 
    <p> 
     <input type="submit" value="Log in" /> 
    </p> 
} 

它不會繼續後動作結果,但總是在獲取一個。我想把這種類型的代碼(最後一個)放在最後我可以設置html,但是我希望它可以在POST Actionresult上進行,有人可以幫我理解爲什麼嗎?

-----------------更新---------------- 這裏生成HTML:

<!doctype html> 
<html lang="it"> 
<head> 
<meta charset="utf-8"> 
<title>Title</title> 
<meta name="robots" content="noindex,nofollow" /> 
<link href="/static/css/login.css" rel="stylesheet" type="text/css" /> 
<link href="/static/css/jquery_ui.css" rel="stylesheet" type="text/css" /> 
<!--[if lt IE 9]><link href="/static/css/lt_ie9.css" rel="stylesheet" type="text/css" /><![endif]--> 
<script src="/static/js/jquery_1_10_2.js"></script> 
<script src="/static/js/jquery_ui.js"></script> 
<script src="/static/js/jquery_ui_function.js"></script> 
</head> 
<body> 
<form> 

<div id="top"> 
    <div class="wrapped"> 
     <div id="logo">TITLE</div> 
    </div> 
</div> 
    <div id="content" class="user_student"> 
     <div class="wrapped"> 
      <div class="login_intro"> 
       <h2>TEST</h2> 
      </div> 
      <div class="login_input"> 
       <p id="error_messages"></p> 
       <h2>THIS ONE MAKES GET REQUEST</h2> 
<form action="/Account/Login" method="post">      <p class="username"><label for="UserName">Nome utente</label> 
          <!--<input id="UserName" name="UserName" type="text"/>--> 
          <input name="LoginUserName" id="LoginUserName"data-validation="required" data-validation-error-msg="Il nome utente &#232; obbligatorio!!!" />; 
         </p> 
         <p class="password"><label for="LoginPassword">Password</label> 
          <input class="text-box single-line" data-val="true" data-val-required="Il campo LoginPassword è obbligatorio." id="LoginPassword" name="LoginPassword" type="text" value="" /> 
         </p> 
         <p><input type="submit" value="Log in" /></p> 
</form>    <p class="hidden">old</p> 
      </div> 
      <div class="login_footer"> 
       <p>FOOTER</p> 
      </div> 
     </div> 
    </div> 
    <h2>THIS ONE MAKE POST REQUEST</h2> 
<form action="/Account/Login?ReturnUrl=%2f" method="post"><div class="editor-label"><label for="LoginUserName">LoginUserName</label></div> 
<div class="editor-field"><input name="LoginUserName" id="LoginUserName"data-validation="required" data-validation-error-msg="" /> <span class="field-validation-valid" data-valmsg-for="LoginUserName" data-valmsg-replace="true"></span></div> 
<div class="editor-label"><label for="LoginPassword">LoginPassword</label></div> 
<div class="editor-field"><input class="text-box single-line" data-val="true" data-val-required="Il campo LoginPassword è obbligatorio." id="LoginPassword" name="LoginPassword" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="LoginPassword" data-valmsg-replace="true"></span></div> 
    <p><input type="submit" value="Log in" /></p> 
</form><script src="/static/form-validator/jquery.form-validator.min.js"></script> 
<script src="/static/js/jquery_form_validator_function.js"></script> 

</form> 
</body> 
</html> 
+1

你是什麼意思「不需要的html」?我沒有看到您的第二個視圖有任何問題。如果你在你的GET登錄操作中放置了'[HttpGet]'屬性,它仍然不起作用嗎? – DavidG

+0

1)使用Chrome開發工具的網絡標籤來驗證它是否是鍵入POST。 2)你把'[HttpGet]'放在'Login()'上了嗎? MVC將使用它找到的第一個滿足條件的動作。如果你沒有標記'[HttpGet]',那麼這意味着它對於GET和POST都有效,因此將使用第一個動作。 – AaronLS

+0

如果你在'AccountController'上有'[Authorize]'屬性,我也很好奇,如果是的話,你是否缺少動作方法的'[AllowAnonymous]'屬性? – DavidG

回答

0

最後我想出了什麼是問題......當然這是世界上最愚蠢的事情。 在渲染的代碼中,在正文後面打開一個窗體,在窗體內部顯示MVC從剃鬚刀視圖中放置的窗體。 感謝大家對@DavidG的特別幫助