0

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;
    }
snapper
  • 210
  • 3
  • 14
  • You need to show the code for `UpdateQuality`. It sound like your updating the `Quality` property of each `ItemLists` - in which case the `HiddenFor()` methods will not show updated values unless you remove them from `ModelState` –  Jul 29 '17 at 23:19
  • Suggest you read the 2nd part of [this answer]() to explain the behavior, but its not clear why you want to return the view and allow the user to continue editing the data they have just finished editing. You should only be returning the view is `ModelState` is invalid. –  Jul 29 '17 at 23:27
  • It's on there now. Was trying to avoid making a huge post. I am enjoying learning to program with MVC but some of the concepts are tough to learn. Thanks! – snapper Jul 29 '17 at 23:28
  • All this is doing is showing products degrading as each day passes. Every time you click the button it is supposed to simulate another day passing. – snapper Jul 29 '17 at 23:30
  • Its a bit unclear what you wanting to do and why you have a whole lot of hidden inputs in the view when nothing at all is editable (its just degrading performance and a malicious user can easily modify all your data. Your POST method should be just getting the data again from the repository and then you run your code to update the values in the repository, and finally redirect back to the GET method so the new values are displayed –  Jul 29 '17 at 23:34
  • Sorry - I left off the link in my previous comment - its [here](https://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) –  Jul 29 '17 at 23:38

0 Answers0