This is my first question on stackoverflow, after many searches, but i still have a strange ASP MVC problem.
I have a View displayingList in a form containings inputs. I want allow the user to remove one or more element, by cheking it and apply on submit.
See the illustration of the problem here:
The Model :
public class MTest
{
public string Nom { get; set; }
public bool Delete { get; set; }
}
The Controller :
public partial class TestController : Controller
{
public virtual ActionResult Index()
{
List<MTest> m = new List<MTest>();
m.Add(new MTest() { Nom = "A", Delete = false });
m.Add(new MTest() { Nom = "B", Delete = false });
m.Add(new MTest() { Nom = "C", Delete = false });
m.Add(new MTest() { Nom = "D", Delete = false });
return View(m);
}
[HttpPost]
public virtual ActionResult Index(List<MTest> model)
{
model.RemoveAll(f => f.Delete == true);
return View(model);
}
}
The View :
@model List<MTest>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@for (int i = 0; i < Model.Count; i++)
{
@Html.EditorFor(model => model[i].Delete)
@Html.EditorFor(model => model[i].Nom, new {
}
<input type="submit" value="Save" />
}
Currently, if i check the 'B' MTest
element (for deleting it), i can see it disapear when i run step by step in the ActionResult Index(List model)
.
But into the View : the result is not the same, and i don't understand why (see illustration).
Can somenone help me ?
Thanks a lot !