Hi! I just started working in MVC and I have a problem:
public class ServicePhone
{
public int AgreementId { get; set; }
public List<PhoneNumber> Phones { get; set; }
public ServicePhone()
{
Phones = new List<PhoneNumber>();
}
public PhoneNumber NewPhone { get; set; }
}
public class PhoneNumber
{
public string Number { get; set; }
public bool IsZoned { get; set; }
public int ZoneOperatorId { get; set; }
public string ZoneOperatorName { get; set; }
public bool IsMultiChannel { get; set; }
public int ChannelsNumber { get; set; }
public bool IsPublic { get; set; }
public bool ToDelete { get; set; }
public bool IsValid()
{
return true;
}
}
in page i write:
<%for (int i = 0; i < Model.Phones.Count; i++)
{%>
<tr>
<td>
<%:Html.TextBoxFor(x => x.Phones[i].Number)%>
</td>
<td>
<%:Html.CheckBoxFor(x=>x.Phones[i].IsZoned) %>
</td>
<td>
<%:Html.TextBoxFor(x => x.Phones[i].ZoneOperatorName)%>
<%:Html.HiddenFor(x=>x.Phones[i].ZoneOperatorId) %>
</td>
<td>
<%:Html.CheckBoxFor(x=>x.Phones[i].IsMultiChannel) %>
</td>
<td>
<%:Html.TextBoxFor(x => x.Phones[i].ChannelsNumber)%>
</td>
<td>
<%:Html.CheckBoxFor(x=>x.Phones[i].IsPublic) %>
</td>
<td>
<%:Html.CheckBoxFor(x=>x.Phones[i].ToDelete) %>
</td>
</tr>
<%
}%>
<input type ="submit" id="addnumber" name="addnumber" value="+" />
<input type ="submit" id="deletenumber" name="deletenumber" value="-" />
controller actions
[System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult Create(int orderNumber)
{
var model = new ServicePhone();
model.Phones.Add(new PhoneNumber
{
ChannelsNumber = 1,
IsMultiChannel = true,
IsPublic = true,
IsZoned = false,
Number = "skldfj",
ZoneOperatorId = 10,
ZoneOperatorName = "name"
});
return View(model);
}
[HttpPost]
[System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult Create(ServicePhone service, string create, int orderNumber, string addnumber, string deletenumber)
{
if (!string.IsNullOrEmpty(addnumber))
{
service.Phones.Add(service.NewPhone);
}
if (!string.IsNullOrEmpty(deletenumber))
{
service.Phones.RemoveAll(x=>x.ToDelete);// = service.Phones.Where(x=>!x.ToDelete).ToList());
}
return View(service);
}
now problem: when i mark n phones to delete, in controller action items deleted successfully, but in returned view to browser with deleted last n items. I mark first 2 items, press button "-", in action deleted first 2 items, in view last 2 items dissapear, first 2 items marked. Why? Please, help me.