【问题标题】:mvc calls httppost alwaysmvc 总是调用 httppost
【发布时间】:2014-03-17 04:45:37
【问题描述】:

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

【问题讨论】:

  • 在表单标签中指定method="post"后尝试
  • 非常感谢.. 成功了....:)

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


【解决方案1】:

您可能需要更改代码行:

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

还有:

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

没有别的了……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    • 2016-08-07
    相关资源
    最近更新 更多