When I submit register, the controller is taking to Http Get instead of HttpPost

时间:2017-04-09 23:20:04

标签: razor asp.net-mvc-5

This is the controller code

public class ConsultantRegisterController : Controller
{
    // GET: ConsultantRegister
    public ActionResult Index()
    {
        using (DataContext db = new DataContext())
        {
            return View(db.Consultant.ToList());
        }
    }

    public ActionResult Register()
    {

        return View();
    }

    [HttpPost]
    [AllowAnonymous]
    public ActionResult Register(Consultant accnt)
    {
        if (ModelState.IsValid)
        {
            using (DataContext db = new DataContext())
            {
                db.Consultant.Add(accnt);
                db.SaveChanges();
            }
            ModelState.Clear();
            accnt = null;
            ViewBag.Message = "Successfully Registration Done";
        }
        return View();
    }

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

    [HttpPost]
    public ActionResult Login(Consultant user)
    {
        using (DataContext db = new DataContext())
        {
            var usr = db.Consultant.Single(u => u.UserId == user.UserId && u.Password == user.Password);
            if(usr!= null)
            {
                Session["ConsultantID"] = usr.ConsultantId.ToString();
                Session["Username"] = usr.UserId.ToString();
                return RedirectToAction("LoggedIn");
            }
            else
            {
                ModelState.AddModelError("", "Username or Password is Wrong");
            }
        }
            return View();
    }

    public ActionResult LoggedIn()
    {
        if (Session["UserID"] != null)
        {
            return View();
        }
        else
        {
            return RedirectToAction("Login");
        }
    }
}

This is the registration form

@model MvcWebForms.Models.Consultant
@{
    ViewBag.Title = "Register";
    Layout = "~/Views/Shared/_HomeLayout.cshtml";
}
<h2>Register</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Consultant</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @if (ViewBag.Message != null)
        {
            <div class="form-group">
                <div class="col-md-10">@ViewBag.Message</div>
            </div>
        }

        <div class="form-group">
            @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EmailId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EmailId, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EmailId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.UserId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.UserId, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.UserId, "", 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" } })
                @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <button class="btn btn-info" type="submit" >Register</button>
                @*<input type="submit" value="Register" class="btn btn-default" />*@
            </div>
        </div>
    </div>
}

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

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
}

I am trying for the whole day but unable to figure it out where am doing wrong. Whenever I try to register it is getting back to [Http GET], but in order to save it should go to [http post].

1 个答案:

答案 0 :(得分:0)

@thoutam srikar

你应该使用

 @using (Html.BeginForm("Action name", "Controller name", FormMethod.Post))