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(); 
} 

간단한 로그인보기를 구현합니다. 지금까지 너무 좋아

@model string 

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

:

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

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

마지막으로 나는 EditorTemplate을 만들어 : 나는 또한 뷰 모델을 만들었습니다. 문제는보기에 있습니다. 내가보기에 넣고 경우

@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가 진행되기를 바란다. 왜 누군가가 나를 이해하는 것을 도울 수 있는가?

는 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)''Login()'에'[HttpGet]'을 넣었습니까? MVC는 기준을 만족하는 것으로 발견 된 첫 번째 작업을 사용합니다. '[HttpGet]'태그가 없다면 그것은 GET과 POST 모두에서 작동한다는 것을 의미하므로 첫 번째 액션을 사용할 것입니다. – AaronLS

+0

'AccountController'에'[Authorize]'속성이 있다면 궁금합니다. 그렇다면 액션 메소드에'AllowAnonymous '속성이 없습니까? – DavidG

답변

0

마침내 나는 문제가 무엇인지 알아 냈다. 물론 그것은 세상에서 가장 바보 같은 일이다. 렌 디드 코드에는 바디의 바로 다음에 열려 있고, 그 안에 MVC가 면도기 뷰에서 가져온 폼이 있습니다. @DavidG에게 도움을 주신 모든 분들께 감사드립니다.

관련 문제