I'm working with MVC and spent whole day getting stuck with this problem. Here is code at the controller:
我和MVC一起工作,花了一整天的时间来解决这个问题。控制器的代码如下:
public class CMController : Controller
{
public ActionResult SignUp()
{
return View("SignUpPage");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SignUp(User u)
{
if (ModelState.IsValid)
{
using (CmdbEntities dc = new CmdbEntities())
{
dc.Users.Add(u);
dc.SaveChanges();
ModelState.Clear();
u = null;
ViewBag.Message = ("Successfully sign on");
}
}
else
{
ViewBag.Message = ("Error");
}
return View("Welcome");
}
}
Here is the SignUpPage.cshtml view:
这是SignUpPage。cshtml视图:
@using (Html.BeginForm("SignUp", "CM", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-group" style="padding:5px 50px">
<label> First name:</label>
<div>
@Html.TextBoxFor(model => model.FirstName, new {@class="form-control", placeholder="Enter first name", ID="FirstName"})
</div>
</div>
<div class="form-group" style="padding:5px 50px">
<label >Last name:</label>
<div>
@Html.TextBoxFor(model => model.LastName, new {@class="form-control", placeholder="Enter first name", ID="LastName"})
</div>
</div>
<div class="form-group" style="padding:5px 50px">
<label for="email">Email:</label>
<div>
@Html.TextBoxFor(model => model.Email, new {@class="form-control", placeholder="Enter email", ID="Email", type="email"})
</div>
</div>
<div class="form-group" style="padding:5px 50px">
<label >User name:</label>
<div>
@Html.TextBoxFor(model => model.UserName, new {@class="form-control", placeholder="Enter user name", ID="UserName"})
</div>
</div>
<div class="form-group" style="padding:5px 50px">
<label for="pwd">Password:</label>
<div>
@Html.TextBoxFor(model => model.Password, new {@class="form-control", placeholder="Enter password", ID="Password", type="password"})
</div>
</div>
<div class="form-group" style="padding:5px 50px">
<label for="pwd">Confirm password:</label>
<div>
@Html.TextBoxFor(model => model.ConfirmPassword, new {@class="form-control", placeholder="Re-enter password", ID="_password", type="password"})
</div>
</div>
<div class="form-group" style="padding:5px 50px">
<div>
<input type="submit" ID="SignUp" class="btn btn-success" value="Sign Up" />
</div>
</div>
}
The problem is, when load page first time, the action SignUp() is called which is correct. But when submit the form, SignUp() is called again instead of the post action SignUp(User u).
问题是,当第一次加载页面时,action SignUp()被调用,这是正确的。但是当提交表单时,会再次调用SignUp()而不是post action SignUp(User u)。
One more problem, even the form has post method, when click submit the url contains input information just like get method, I don't understand.
还有一个问题,表单也有post方法,当点击submit url时,url就像get方法一样包含输入信息,我不明白。
Here is the page html source of form tag.
这是表单标记的html页面源。
<form action="/CM/SignUp" method="post"><input name="__RequestVerificationToken" type="hidden" value="VJ0YcQr1Z-l3Qo717pRpZNT-QtL59G2EJXy2JQL8NFnOm-XoNnAD-8T-Itz3i1EboGj0bhpBf2G26ifxm4F5ZRyKimkOyZW1AxZ3ckPNuRk1" />
Please help, thank you.
请帮助,谢谢。
1 个解决方案
#1
1
Can you please try adding [AllowAnonymous]
你能试试添加[AllowAnonymous]吗?
to the post method?
post方法?
Did you activate global authorization anywhere System.Web.Mvc.AuthorizeAttribute ?
你是否激活了全局授权? System.Web.Mvc。AuthorizeAttribute吗?
#1
1
Can you please try adding [AllowAnonymous]
你能试试添加[AllowAnonymous]吗?
to the post method?
post方法?
Did you activate global authorization anywhere System.Web.Mvc.AuthorizeAttribute ?
你是否激活了全局授权? System.Web.Mvc。AuthorizeAttribute吗?