回发方法不能在MVC4中点击按钮进行调用

时间:2014-09-21 10:54:46

标签: asp.net asp.net-mvc asp.net-mvc-4

我在mvc4中创建了一个没有_layout.cshtml的登录页面,并添加了两个带有按钮的文本框。但是,当我点击按钮时,它没有回复。我尝试过使用断点。请帮助。我的代码

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

1 个答案:

答案 0 :(得分:1)

您必须在表单中输入输入元素和提交按钮。为此,您可以通过这种方式使用表单Html.BeginForm()的asp.net mvc Html Helper Extension:

@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

有很多重载