I'm current a WebForms developer that is trying to move to MVC. I'm super excited about MVC and I'm really have fun but I'm running into a weird issue. So what I'm trying to do is create an advanced editor for a "widget". I've posted the code below.
Everything appears to work fine when you add the first 4-5 items but the problem occurs when you delete the 2nd item. Here is a visual example.
First add the 4 values.
But the problem occurs when we delete the 2nd value. We end up with this...
What I cannot seem to understand is why does this property act different between the two following lines of code.
@Model.Values[i]
@Html.TextBoxFor(m => m.Values[i])
My guess is that the @Model and (m =>m) do not reference the same object?
Here is my widget class.
public class Widget
{
#region Constructor
public Widget()
{
ID = 0;
Name = string.Empty;
Values = new List<string>();
}
#endregion
#region Properties
[Required]
[Display(Name = "ID")]
public int ID { get; set; }
[Required]
[Display(Name = "Name")]
public string Name { get; set; }
[Required]
[Display(Name = "Values")]
public List<string> Values { get; set; }
#endregion
}
My controller looks like this.
public ViewResult EditWidget(int id)
{
return View(_widgets.GetWidgetByID(id));
}
[HttpPost]
public ActionResult EditWidget(Widget widget)
{
if (!TryUpdateModel(widget))
{
ViewBag.Message = "Error...";
return View(widget);
}
if (Request.Form["AddWidgetValue"] != null)
{
widget.Values.Add(Request.Form["TextBoxWidgetValue"]);
return View("EditWidget", widget);
}
if (Request.Form["DeleteWidgetValue"] != null)
{
widget.Values.Remove(Request.Form["ListBoxWidgetValues"]);
return View("EditWidget", widget);
}
_widgets.UpdateWidget(widget);
_widgets.Save();
return RedirectToAction("Index");
}
And finally my view.
@model MvcTestApplication.Models.Widget
@{
ViewBag.Title = "EditWidget";
}
<h2>EditWidget</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Widget</legend>
@Html.HiddenFor(model => model.ID)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
@for (var i = 0; i < Model.Values.Count; i++)
{
@Model.Values[i]
@Html.TextBoxFor(m => m.Values[i])
@Html.HiddenFor(m => m.Values[i])
<br />
}
@Html.ListBox("ListBoxWidgetValues", new SelectList(Model.Values), new { style = "width: 100%" })<br />
@Html.TextBox("TextBoxWidgetValue", string.Empty, new { style = "width: 100%" })
<input type="submit" value="Add" id="AddWidgetValue" name="AddWidgetValue" class="submitButton" />
<input type="submit" value="Delete" id="DeleteWidgetValue" name="DeleteWidgetValue" class="submitButton" />
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>