回发方法MVC4中的按钮点击方法没有得到调用

问题描述:

我已经在mvc4中创建了一个没有_layout.cshtml的登录页面,并添加了两个带有按钮的文本框。但是,当我点击按钮它没有得到回发。我曾尝试使用断点。请帮助。我的代码回发方法MVC4中的按钮点击方法没有得到调用

@model MapProjectMVC.Models.LoginModel 

@{ 
    Layout = null;  
} 

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Special Spots</title> 
    @Styles.Render("~/Content/css") 
    @Scripts.Render("~/bundles/modernizr") 
</head> 
<body> 
    <div class="loginBox"> 
     <div class="loginHead"> 
     <img src="img/logo.png" alt="Special Spots - responsive admin panel" title="Special Spots - responsive admin panel" /> 
    </div> 
    <div class="control-group"> 
     <label for="inputEmail"> 
      User Name</label> 
     @Html.TextBoxFor(a => a.UserName) 
    </div> 
    <div class="control-group"> 
     <label for="inputPassword"> 
      Password</label> 
     @Html.TextBoxFor(a => a.Password) 
    </div> 
    <div class="control-group" style="margin-bottom: 5px;"> 
    </div> 
    <div class="form-actions"> 
     <button type="submit" class="btn btn-block"> 
      Sign in</button> 
    </div> 
</div> 
    @Scripts.Render("~/bundles/jquery") 
</body> 
</html> 


[HttpGet] 
    public ActionResult Login() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult Login(LoginModel LM) 
    {   
     var UserId = new ObjectParameter("userId",typeof(string)); 
     var res = new ObjectParameter("res",typeof(Int32)); 
     int i = ssc.ValidateAdminLogin(LM.UserName, LM.Password, UserId, res); 
     if (Convert.ToInt32(res) == 1) 
     { 

     } 
     else 
     { 
      ModelState.AddModelError("", "Login details are wrong."); 
     } 

     return View(LM); 
    } 
+3

放里面形式 – 2014-09-21 10:56:08

+0

@EhsanSajjad为我创造了另一个页面使用默认的_layout.cshtml,不需要添加表单。所以清楚为什么要在这个页面添加表单标签。 – Raghubar 2014-09-21 11:01:10

+1

在asp.net mvc中,无论何时我们需要发布值,我们必须使用表格 – 2014-09-21 11:04:53

你必须把输入元素和提交按钮的形式。为了这个目的,你可以使用asp.net的MVC HTML辅助扩展形式Html.BeginForm()这样:

@using(Html.BeginForm()) 
{ 
<div class="loginBox"> 
     <div class="loginHead"> 
     <img src="img/logo.png" alt="Special Spots - responsive admin panel" title="Special Spots - responsive admin panel" /> 
    </div> 
    <div class="control-group"> 
     <label for="inputEmail"> 
      User Name</label> 
     @Html.TextBoxFor(a => a.UserName) 
    </div> 
    <div class="control-group"> 
     <label for="inputPassword"> 
      Password</label> 
     @Html.TextBoxFor(a => a.Password) 
    </div> 
    <div class="control-group" style="margin-bottom: 5px;"> 
    </div> 
    <div class="form-actions"> 
     <button type="submit" class="btn btn-block"> 
      Sign in</button> 
    </div> 
</div> 
} 

还有的Html.BeginForm()许多重载,See all overloads here