Reputation: 23
Why am I not cleared form ? ModelState.Clear(); is not work! in updated form sends the data again. Tried many different solutions but all in vain, so hope to find here a solution to this problem. Help me please! "Controller"
public ActionResult Yandex()
{
ViewData["sucsess"]=" ";
return View();
}
[HttpPost]
public ActionResult Yandex(LoginModel model)
{
if (model.option1)
{
md.Modules_Update(1,1);
ViewData["sucsess"] = "• Дані успішно збережені!";
}
else
{
md.Modules_Update(1, 0);
ViewData["sucsess"] = "• Дані успішно збережені!";
}
ModelState.Clear();
return View( );
}
"View"
@using (Html.BeginForm())
{
Settings exit = new Settings();
<span class="property">Модуль включений: </span>
<span class="property">Так</span>
if (exit.Modules(1).ToString() == "1")
{
@Html.RadioButtonFor(m => m.option1, true, new { @checked = "checked", id = "1", @class="radio" })
}
else
{
@Html.RadioButtonFor(m => m.option1, true, new { id = "1", @class="radio" })
}
<span class="property">Ні</span>
if (exit.Modules(1).ToString() == "0")
{
@Html.RadioButtonFor(m => m.option1, false, new { @checked = "checked", id = "0", @class="radio" })
}
else
{
@Html.RadioButtonFor(m => m.option1, false, new { id = "0", @class="radio" })
}
<input type="submit" value="Зберегти" class="save" />
}
<span class="property_col">@ViewData["sucsess"]</span>
Upvotes: 1
Views: 2131
Reputation: 1039298
Values are still present in the POST request. The proper way to do this is to redirect to your GET action:
[HttpPost]
public ActionResult Yandex(LoginModel model)
{
if (model.option1)
{
md.Modules_Update(1,1);
TempData["sucsess"] = "• Дані успішно збережені!";
}
else
{
md.Modules_Update(1, 0);
TempData["sucsess"] = "• Дані успішно збережені!";
}
return RedirectToAction("Yandex");
}
Also notice that I have used TempData to transport the message during the redirect. So you might need to adapt your view accordingly:
<span class="property_col">@TempData["sucsess"]</span>
Upvotes: 1