Razor 当我提交注册表时,控制器将使用HTTPGET而不是HttpPost

Razor 当我提交注册表时,控制器将使用HTTPGET而不是HttpPost,razor,asp.net-mvc-5,Razor,Asp.net Mvc 5,这是控制器代码 public class ConsultantRegisterController : Controller { // GET: ConsultantRegister public ActionResult Index() { using (DataContext db = new DataContext()) { return View(db.Consultant.ToList());

这是控制器代码

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");
        }
    }
}
这是登记表

@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>
}
@model MvcWebForms.Models.Consultant
@{
ViewBag.Title=“寄存器”;
Layout=“~/Views/Shared/_homeloayout.cshtml”;
}
登记
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
顾问

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @如果(ViewBag.Message!=null) { @查看包。留言 } @LabelFor(model=>model.FirstName,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.FirstName,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.FirstName,“,new{@class=“text danger”}) @LabelFor(model=>model.LastName,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.LastName,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.LastName,“,new{@class=“text danger”}) @LabelFor(model=>model.EmailId,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.EmailId,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.EmailId,“,new{@class=“text danger”}) @LabelFor(model=>model.UserId,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.UserId,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.UserId,“,new{@class=“text danger”}) @LabelFor(model=>model.Password,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Password,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Password,“,new{@class=“text danger”}) @LabelFor(model=>model.ConfirmPassword,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.ConfirmPassword,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.ConfirmPassword,“,new{@class=“text danger”}) 登记 @**@ } @ActionLink(“返回列表”、“索引”) }
我一整天都在努力,但无法找出我做错了什么。每当我尝试注册时,它都会返回到[Http GET],但为了保存它,应该转到[Http post]。

@thoutam srikar

你应该使用

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

也试过了,但没用,还是要去httpGetsubmission@thoutamsrikar添加Manish建议的配置后,在浏览器的开发工具中观察网络流量。它发布到正确的URL了吗?@mason抱歉,mason不知道有什么进展tools@thoutam在浏览器中点击F12以打开开发工具。@当您在开发工具中查看它时,它是POST还是GET?