admin管理员组文章数量:1287517
I'm implementing an asp mvc5 application and having an issue with post operation and query string parameters.
My page url is http://localhost/site/person/edit?personId=20
[HttpGet]
public ActionResult Edit(int personID)
{
// ....
return View(person);
}
In this page contain submit button and it will do the post method. In post method, if Model.Isvalid get false, I return to the view with Model.
[HttpPost]
public ActionResult Edit(Person person)
{
if (ModelState.IsValid)
{
//....
return RedirectToAction("Index", "Dashboard", new { area = "" });
}
else
{
return View(person);
}
}
This works correctly.
But the issue is once the Model.IsValid get false, it will e to the view but with no query string parameters. Then URL is like http://localhost/site/person/edit
Is there a way to get the URL with query string parameters.
I'm implementing an asp mvc5 application and having an issue with post operation and query string parameters.
My page url is http://localhost/site/person/edit?personId=20
[HttpGet]
public ActionResult Edit(int personID)
{
// ....
return View(person);
}
In this page contain submit button and it will do the post method. In post method, if Model.Isvalid get false, I return to the view with Model.
[HttpPost]
public ActionResult Edit(Person person)
{
if (ModelState.IsValid)
{
//....
return RedirectToAction("Index", "Dashboard", new { area = "" });
}
else
{
return View(person);
}
}
This works correctly.
But the issue is once the Model.IsValid get false, it will e to the view but with no query string parameters. Then URL is like http://localhost/site/person/edit
Is there a way to get the URL with query string parameters.
Share Improve this question asked May 8, 2014 at 11:16 cp100cp100 1,5036 gold badges20 silver badges36 bronze badges 03 Answers
Reset to default 9I found the answer on another SO post: Query string param is missed when form validation fail
The solution for me was to concatenate the dropped value directly onto the form tag.
Using the BeginForm() method:
@using (Html.BeginForm("Edit", "Person", routeValues: new {personID = Model.id}, method: FormMethod.Post))
Yes. Don't supply (200 OK) content in answer to a POST, but redirect with a query string to a GET method.
Supplying content in a POST is almost always a bad idea because when the user refreshes the page, the POST gets made again (along with a confusing dialog in many users agents that asks the user if they are sure the want to resend the POST)
Add your ID to the POST method also:
[HttpPost]
public ActionResult Edit(int personID, Person person)
{
if (ModelState.IsValid)
{
//....
return RedirectToAction("Index", "Dashboard", new { area = "" });
}
else
{
return View(person);
}
}
Your view should also POST to the same URL as it is on:
@using (Html.BeginForm())
本文标签:
版权声明:本文标题:javascript - Query string parameters get removed after do the post and return to the same page in asp.net mvc - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741285346a2370239.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论