mercredi 4 novembre 2015

Asp.Net MVC 5, View not posting back to the controller

I am creating a simple login form using Asp.Net MVC 5. Everything is fine but when I click on submit button after giving User Id and Password, the view does not go back to the desired Controller Action (LogIn). Here is the Action:

        [HttpPost]
        public ActionResult LogIn(User user)
        {
            var auth_user = CheckAuthentication(user);
            if(auth_user!=null)
            {
                Session["user"] = new User() { UserId = user.UserId, Name = user.Name };
                return RedirectToAction("Index", "User");
            }
            return View();
        }
        [AllowAnonymous]
        public ActionResult LogIn()
        {
            return View();
        }

and the view:

@model FinancialManagement.Models.User

@{
    ViewBag.Title = "LogIn";
}

<h2>LogIn</h2>

@using (Html.BeginForm("LogIn", "User", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>User</h4>
        <hr />
        @Html.ValidationSummary(true)
        <div class="form-group">
            @Html.LabelFor(model => model.UserId, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.UserId)
                @Html.ValidationMessageFor(model => model.UserId)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Password)
                @Html.ValidationMessageFor(model => model.Password)
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Log In" class="btn btn-default" />
            </div>
        </div>
    </div>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Aucun commentaire:

Enregistrer un commentaire