表单提交在MVC 5项目中不起作用

时间:2018-01-08 14:25:34

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

我的提交表单按钮不起作用.. 当我按下登录按钮时没有发生任何事情...... 我在我的代码中设置了断点,并在提交按钮单击后未调用登录发布操作。 我可以使用JQuery ajax代码解决我的问题,但我不想使用JQuery Ajax提交Form ..我想了解并解决这个MVC问题 谢谢......

用户模型:

namespace Models
{
    public class User : BaseEntity
    {
        public User() : base()
        {
        }

        [System.ComponentModel.DataAnnotations.Required]
        public string Username { get; set; }

        [System.ComponentModel.DataAnnotations.Required]
        [System.ComponentModel.DataAnnotations.DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
        public string Password { get; set; }
    }
}

BaseEntity类只有Id(System.Guid)属性

控制器:

public class AdminController : Infrastructure.BaseController
{
    [System.Web.Mvc.HttpGet]
    public System.Web.Mvc.ActionResult Index()
    {
        return View();
    }

    [System.Web.Mvc.HttpGet]
    public System.Web.Mvc.ActionResult Login()
    {
        return View();
    }

    [System.Web.Mvc.HttpGet]
    public System.Web.Mvc.ActionResult Edit()
    {
        return View();
    }

    [System.Web.Mvc.HttpPost]
    [System.Web.Mvc.ValidateAntiForgeryToken]
    public System.Web.Mvc.ActionResult Login([System.Web.Mvc.Bind(Include = "Id,Username,Password")] Models.User user)
    {
        string password = AAk.Security.Hashing.GetMD5(user.Password);

        Models.User oUser = MyDatabaseContext.Users
            .Where(current => current.Username.Contains(user.Username))
            .Where(current => current.Password == password)
            .FirstOrDefault();

        if(oUser != null)
        {
            System.Web.Security.FormsAuthentication.SetAuthCookie(user.Username, false);
            Session["AdminUserId"] = user.Id;
            return (RedirectToAction("Edit", "Admin"));
        }
        else
        {
            //ModelState.AddModelError(string.Empty, "Login Failed!");
            PageMessages.Add(new Infrastructure.PageMessage(Infrastructure.PageMessage.Types.Error, "Login Failed!"));
        }

        return (View(model: user));
    }
}

Login.cshtml

@model Models.User

@{
    ViewBag.Title = "Login";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Login</h2>

@using (Html.BeginForm("Login", "Admin", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        @Html.Partial(partialViewName: "~/Views/Shared/_PageMessages.cshtml")
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)

        <div class="form-group">
            @Html.LabelFor(model => model.Username, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Username, new { htmlAttributes = new { @class = "form-control", @id = "username" } })
                @Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control", @id = "password" } })
                @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Login" class="btn btn-default" id="login-button" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

0 个答案:

没有答案