mvc总是调用httppost

时间:2014-03-17 04:45:38

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

我正在创建一个登录页面: 当我进入登录页面时,应该呈现登录视图。在提交时,应触发http POST,该主页应使用用户名呈现主页。我的问题是,无论我做什么,都是在提交时调用的http GET方法。我是mvc的新手,想知道我做错了什么。为什么调用http POST方法的程序不提交按钮单击

//             My login Class(I mean the model looks like this



      using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;


        namespace a.Models
        {
        public class Login
        {
            public string username { get; set; }
            public string password { get; set; }
        }
        }

                         // Controller looks like this:



namespace b.Controllers
{
public class LoginController : Controller
{
    [HttpPost]
    public ActionResult Login(Login Ls)
    {
        return View();
    }

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

}
}

                   // Login view look like this

        @model a.Models.Login

        @{
        ViewBag.Title = "Login Page";
        }


       <html>
       <head>
        <title>Login Here</title>
        <link href="@Url.Content("~/Content/cssLogin.css")" rel="stylesheet" type="text/css" />

    </head>
    <body>
        <div id="bg">
            <img src="~/Photos/login2.jpg" />

        </div>
        <form class="form-2">

            <p class="float">
                @*<label for="login"><i class="icon-user"></i>Username</label>
                <input type="text" name="login" placeholder="Username or email">*@

                @Html.LabelFor(model => model.username, new { @class = "icon-use" })
                @Html.TextBoxFor(model => model.username, new { data_bind = "value: username" })
            </p>
            <p class="float">
                @*<label for="password"><i class="icon-lock"></i>Password</label>
                <input type="password" name="password" placeholder="Password" class="showpassword">*@

                @Html.LabelFor(model => model.password, new { @class = "icon-lock" })
                @Html.PasswordFor(m => m.password, new { data_bind = "value: password", placeholder = "Password" })
            </p>
            <p class="clearfix">

                <input type="submit" value="Log in">


            </p>
        </form>
    </body>

    </html>

                                      // Home View looks like this


     @model a.Models.Login

        @{
        ViewBag.Title = "Home";
        }

       <h2>Home</h2>

        <h2>This is University Home page</h2>

        @Model.username

1 个答案:

答案 0 :(得分:0)

您可能需要更改代码行:

public ActionResult Login()
{
    return View(new Login());
}

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

没有别的......