Asp.net MVC - MVC总是调用httppost



我正在创建一个登录页面:当我转到登录页面时,应该呈现登录视图。在提交时,应该触发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

您可能需要更改代码行如下:

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

:

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

最新更新