Reputation: 1287
I have a small form which will just have two fields i.e. key(hidden) and password, and when user fills the password clicks on this submit button i need to call an action method and update something in DB. I am not able to get this working with the current code i have
Model:-
public class AccountPasswordResetModel
{
public string Key { get; set; }
public string Password { get; set; }
}
Controller
public class AccountPasswordResetController:Controller
{
public ActionResult Index()
{
return View("~/Areas/Home/Views/AccountPasswordReset/Index.cshtml");
}
public ActionResult Update(AccountPasswordResetModel AccountPasswordResetModel )
{
return Redirect("http://www.example.org");
}
}
CSHTML :-
@model Verivox.Home.Presentation.Model.AccountPasswordResetModel
@using(@Html.BeginForm("Update","AccountPasswordReset",FormMethod.Post))
{
@Html.TextBoxFor(x=>x.Key)
@Html.TextBoxFor(x=>x.Password)
<input type="submit" value="Update"/>
}
And the route is registered initially using Fluent Routing as below
var accountPasswordResetRoute = new LowercaseRoute("partners/accountVerification/{verificationKey}");
context.MapNamedRoute(accountPasswordResetRoute.WithDefaultAction<AccountPasswordResetController>(t => t.Index()), "test");
So the Index actionis getting executed fine, but when i POST the form, Index() is getting executed again.
What is wrong here?
Upvotes: 0
Views: 5089
Reputation: 179
try
[HttpPost]
public ActionResult Update(AccountPasswordResetModel AccountPasswordResetModel )
{
return Redirect("http://www.example.org");
}
Adding [HttpPost]
before action.
update edit view
@using(@Html.BeginForm("Update","AccountPasswordReset",FormMethod.Post))
{
@Html.EditorFor(model =>model.Key)
@Html.EditorFor(model =>model.Password)
<input type="submit" value="Update"/>
}
or add into controller
[HttpPost]
public ActionResult Update(string Key,string Password )
{
return Redirect("http://www.example.org");
}
Upvotes: 1