0

I'm having this issue where I'm unable to update a data. It will go to the edit page but when I click update it just redirects to the other page and there's no data shown. How can get the updated data to be shown when it is redirected. Update question with class issue

controller:

 private Issue getIssue
    {
        get
        {
            Issue issue = (Issue)Session["Issue"];
            if (issue == null)
            {
                issue = new Issue();
                Session["Issue"] = issue;
            }
            return issue;

        }

    }


    public ActionResult Edit(int id)
    {

        getIssue.item = getIssue.items[id - 1];//Returns the requested item for editing
        return View(getIssue);
    }

    [HttpPost]
    public ActionResult Edit(Issue issue)
    {
        int indx = issue.item.lineNum - 1;
        getIssue.items[indx] = issue.item;
        //return View(getIssue);
        return RedirectToAction("IssueItem", "Issue");
   }

View:

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Issue</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })



    <fieldset>
        <div class="form-horizontal">

            <hr />
            <div class="form-group">
                <div class="form-group">
                    @Html.LabelFor(model => model.item.itemNumber, htmlAttributes: new { @class = "control-label col-md-2" })
                    @Html.TextBoxFor(model => model.item.itemNumber, null, new { @id = "itemNumber", @class = "form-control", @readonly = "readonly", })
                    @Html.ValidationMessageFor(model => model.item.itemNumber, "", new { @class = "text-danger" })
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.item.description, htmlAttributes: new { @class = "control-label col-md-2" })
                    @Html.TextBoxFor(model => model.item.description, null, new { @id = "description", @class = "form-control", @readonly = "readonly", })
                    @*  @Html.EditorFor(model => model.item.description, new { @class = "control-label col-md-2", @id = "txtItem" })*@
                    @Html.ValidationMessageFor(model => model.item.description, "", new { @class = "text-danger" })
                </div>


<div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Update" name="Update" class="btn btn-default" />
                    <input type="button" value="Cancel" name="Cancel" onclick="location.href='@Url.Action("IssueItem","Issue")' " class="btn btn-default" />
                </div>

            </div>

3 Answers3

0

Assuming that the getIssue.items[indx] = issue.item; updates the item,

After updating a item the server must:

  1. Know what item was update
  2. Redirect the user to the updated item.

The redirect RedirectToAction("IssueItem", "Issue"); is redirecting to a generc URL, not a specific one. That is the problem.

  1. Lets get what item was updated. You don't use Id but you do use indx on the [HttpPost] method. so we know that indx has the identification number of the item.

  2. Lets redirect the user to the item page. I don't know your project, so I will assume that there is a Detail page. We get the indx variable and pass it as arg to the redirect: for instance RedirectToAction("Detail",new {id = indx}); This should work.

Daniel Santos
  • 14,328
  • 21
  • 91
  • 174
  • could you give a snippet because id is not apart of the Edit(Issue issue) so that variable would give an error –  Nov 01 '17 at 17:27
  • I changed the answer, you are using `indx` instead of id – Daniel Santos Nov 01 '17 at 17:28
  • ok so when it returns it doesn't show the data and this in the url localhost:50616/Issue/IssueItem/0 –  Nov 01 '17 at 18:54
  • Hey @Daniel I'm stuck as to how to get the updated on the page –  Nov 01 '17 at 21:07
  • the issue is still there once i click update it goes back without the data –  Nov 02 '17 at 19:53
  • Hey Daniel do you have time to look on this issue for me please. I'm still having the issue when it redirects it doesn't show the updated data –  Nov 03 '17 at 16:35
  • The data isn’t shown outdated? Or no data is shown.? – Daniel Santos Nov 03 '17 at 16:42
  • I’m sorry. I need much more details to understand what is going wrong with your application. At this point I strongly advise you to try to understand how this app works and maybe take a small asp.net mvc course. You will find very good ones . – Daniel Santos Nov 03 '17 at 17:09
  • I understand how it works, but just the part that the data is being "erased" once I return –  Nov 03 '17 at 17:19
  • Is it possible for you to show me a snippet of what would happen when it returns –  Nov 03 '17 at 17:20
  • Hey @Daniel is it possible to give me some assistance on this question, after I thought i have figured out this problem, I'm getting an index error [id - 1] as id returns 0. How can I fix this issue –  Nov 23 '17 at 17:39
0

First thing I noticed is that you are not saving the identifier of your object to be updated in the view, you should use Html.HiddenFor, like this:

@Html.HiddenFor(x => x.lineNum)

I assumed that lineNum is the identifier of your object.

Tiago Ávila
  • 2,737
  • 1
  • 31
  • 34
  • I'm having an issue relating to this question, I now realized that the identifier object returns 0 and gives me an error. Question, can use something else instead of using line Num to identify the object. Cause on some occasions it only picks up the first item as id is always one –  Nov 24 '17 at 15:20
  • @TroyBrown I think you can also use the index of the element in your list. Or you can use a hash code, like: var hash = myObject.GetHashCode(); – Tiago Ávila Nov 24 '17 at 18:46
  • how would I use the index element in my list. –  Nov 27 '17 at 15:17
0

The issue was that I'm using a session to clear out the fields so each time it redirects that session was cleared out; I've since remove and it works fine