0

I wanted to redirect "Edit" Actionlink to a another page from Index.cshtml page and the directions in the tab shows "http://localhost:49712/Home/Edit/id". The data are shown in "Index.cshtml" in a table but not all data is shown (just some of it). Then, when I want to edit the data, the edit page shows "HTTP Error 404.0 - Not Found". I think this is because of the attributes got clash or anything?

This is the "Index.cshtml" for the table:-

foreach (var item in Model)
        {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.ID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.NUMBER)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TITLE)
            </td>

            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.ID}) |
                @Html.ActionLink("Details", "Details", new { id = item.ID}) |
                @Html.ActionLink("Delete", "Delete", new { id = item.ID})
            </td>
        </tr>
        }

This is the edit part in controller":-

// GET: Home/Edit/5
        public async Task<ActionResult> Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Tests test= await db.Test.FindAsync(id);
            if (test== null)
            {
                return HttpNotFound();
            }
            return View(test);
        }

        // POST: Home/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "ID,YEAR,LANGUAGE,COUNTRY,SUBJECT,NUMBER,TITLE] Tests test)
        {
            if (ModelState.IsValid)
            {
                db.Entry(test).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return View(test);
        }

This is my model:-

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class Tests
    {
        public string ID{ get; set; }
        public Nullable<int> YEAR { get; set; }
        public string LANGUAGE { get; set; }
        public string COUNTRY { get; set; }
        public string SUBJECT { get; set; }
        public string NUMBER { get; set; }
        public string TITLE { get; set; }
     }
}

As you can see, some of the attributes does not shown in data table in "Index.cshtml", but all of them will be shown in edit page. How can I do this? Any help from anyone would be highly appreciated.

lara
  • 83
  • 1
  • 13
  • Is the link literally http://localhost:49712/Home/Edit/id or did you leave something out at the end? – HazardousGlitch May 31 '19 at 02:10
  • What url do you see in browser address bar when you click on edit link? – Chetan May 31 '19 at 02:11
  • it actually like this, "http://localhost:54048/Home/Edit/1" **id** is the id number, for example. @HazardousGlitch – lara May 31 '19 at 02:29
  • @ChetanRanpariya the ones I mentioned in comment above (: – lara May 31 '19 at 02:30
  • 1
    I think your code is taking the path `if (test== null)` and then `return HttpNotFound();` -- a quick way to find out for sure is to change the return to use this overload `return HttpNotFound($"Test ID {id} was not found");` or step trace using the debugger to see if that condition is met. – David Tansey May 31 '19 at 02:33
  • Take a look at the accepted answer in this post: https://stackoverflow.com/questions/4985550/how-to-return-a-view-for-httpnotfound-in-asp-net-mvc-3 – David Tansey May 31 '19 at 02:50
  • That way does not worked for me, it still returns Error Http 404. :( @DavidTansey – lara May 31 '19 at 03:08
  • I dont understand how that link related to my problem? @DavidTansey – lara May 31 '19 at 03:13
  • The accepted answer in that post describes the challenges of using the `HttpNotFound()` method -- it does not do what most people expect it will. Did you try putting a breakpoint on that `if` statement to see if `test==null` or not? – David Tansey May 31 '19 at 03:18
  • Yes I did try that but it still shows Error Http 404, on my browser after i click edit action link. @DavidTansey – lara May 31 '19 at 03:49
  • Add code edit view or also check have you created edit view at proper place. – A.M. Patel May 31 '19 at 05:12
  • I'm using mvc5 razor with entity framework so it'll create automatically inside view folder. Usually it synchronizes the controller as well. I dont know why it did not worked this time. @A.M.Patel – lara May 31 '19 at 06:56

0 Answers0