2014-09-21 64 views
0

我已經在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

回答

1

你必須把輸入元素和提交按鈕的形式。爲了這個目的,你可以使用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