I have a table in a view that I collect data from and send to my controller where it goes through an UpdateData function (it just multiplies the data). Once updated I return it to the view and it displays updated correctly in the table. Once I click the submit button again it sends the table data back to the view but its the original data and not the updated set? Am I missing something about collecting table data in MVC views? Any help is greatly appreciated.
@model SDSProgrammingChallenge.Models.ItemList
@{
ViewBag.Title = "Welcome to The Inn";
}
@using (Html.BeginForm("UpdateQuality", "Home", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
<div class="row">
<div class="form-group">
<table class="table table-bordered">
<tr>
<th>@Html.DisplayNameFor(m => m.ItemLists.FirstOrDefault().Name)</th>
<th>@Html.DisplayNameFor(m => m.ItemLists.FirstOrDefault().SellIn)</th>
<th>@Html.DisplayNameFor(m => m.ItemLists.FirstOrDefault().Quality)</th>
</tr>
@for (int i = 0; i < Model.ItemLists.Count(); i++)
{
<tr>
<td>@Html.DisplayFor(m => m.ItemLists[i].Name)</td>
<td>@Html.DisplayFor(m => m.ItemLists[i].SellIn)</td>
<td>@Html.DisplayFor(m => m.ItemLists[i].Quality)</td>
@Html.HiddenFor(m => m.ItemLists[i].Name)
@Html.HiddenFor(m => m.ItemLists[i].SellIn)
@Html.HiddenFor(m => m.ItemLists[i].Quality)
</tr>
}
</table>
<div class="form-group">
<div style="margin-top: 50px">
<input type="submit" class="btn btn-primary" value="Advance 1 Day"/>
</div>
</div>
</div>
</div>
}
And the controller
public ActionResult Index()
{
ItemList itemList = Helper.Helper.GetData();
return View("Index", itemList);
}
public ActionResult UpdateQuality(ItemList itemList)
{
itemList = Helper.Helper.UpdateQuality(itemList);
return View("Index", itemList);
}
Adding the function per request.
public static ItemList UpdateQuality(ItemList Items)
{
for (var i = 0; i < Items.ItemLists.Count; i++)
{
// If NOT Aged Brie AND NOT Backstage Passes
if (Items.ItemLists[i].Name != "Aged Brie" && Items.ItemLists[i].Name != "Backstage passes to a TAFKAL80ETC concert")
{
if (Items.ItemLists[i].Quality > 0)
{
if (Items.ItemLists[i].Name != "Sulfuras, Hand of Ragnaros")
{
Items.ItemLists[i].Quality = Items.ItemLists[i].Quality - 1;
}
}
}
// If Aged Brie or Backstage
else
{
if (Items.ItemLists[i].Quality < 50)
{
Items.ItemLists[i].Quality = Items.ItemLists[i].Quality + 1;
if (Items.ItemLists[i].Name == "Backstage passes to a TAFKAL80ETC concert")
{
if (Items.ItemLists[i].SellIn < 11)
{
if (Items.ItemLists[i].Quality < 50)
{
Items.ItemLists[i].Quality = Items.ItemLists[i].Quality + 1;
}
}
if (Items.ItemLists[i].SellIn < 6)
{
if (Items.ItemLists[i].Quality < 50)
{
Items.ItemLists[i].Quality = Items.ItemLists[i].Quality + 1;
}
}
}
}
}
///// If NOT Sulfuras /////
if (Items.ItemLists[i].Name != "Sulfuras, Hand of Ragnaros")
{
Items.ItemLists[i].SellIn = Items.ItemLists[i].SellIn - 1;
}
///// If Sellin less than 0 //////
if (Items.ItemLists[i].SellIn < 0)
{
// If NOT Aged Brie
if (Items.ItemLists[i].Name != "Aged Brie")
{
if (Items.ItemLists[i].Name != "Backstage passes to a TAFKAL80ETC concert")
{
if (Items.ItemLists[i].Quality > 0)
{
if (Items.ItemLists[i].Name != "Sulfuras, Hand of Ragnaros")
{
Items.ItemLists[i].Quality = Items.ItemLists[i].Quality - 1;
}
}
}
else
{
Items.ItemLists[i].Quality = Items.ItemLists[i].Quality - Items.ItemLists[i].Quality;
}
}
// If Aged Brie
else
{
if (Items.ItemLists[i].Quality < 50)
{
Items.ItemLists[i].Quality = Items.ItemLists[i].Quality + 1;
}
}
}
}
return Items;
}