0

Again, after tiresome attempts to solve this annoying issue, I am back here. I am experiencing an "Object moved here" on my view in my MVC5 application after some return RedirectToAction("Index", "Roles", model); action on my controller.

I have considered solutions from the following links:

  1. RedirectToAction and "Object moved to here" error

  2. ASP.NET MVC Website: Object Moved to Here

  3. https://forums.asp.net/t/1643235.aspx?+Object+moved+to+here+problem but none are applicable to my problem.

Before anyone marks this as a duplicate, please consider the scenario below:

But now the weird thing is that after I have clicked on the 'here' link and it now renders my view - it has a horizontal split screen showing the correct display of my view on top and below the view that throws an Http Error Code - in this case its Error Code 500 : Internal Server Error.

The reason why I am getting the http status code error is due to this unanswered question here (one of many) :

Failed to load resource: Uncaught TypeError: $(...).select2 is not a function

But that is besides the point right now.

Controller:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "Name,Description")] ApplicationRole model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var role = new ApplicationRole()
                    {
                        Name = model.Name,
                        Description = model.Description
                    };

                    var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(db));
                    var result = await roleManager.CreateAsync(role);
                    if (result.Succeeded)
                    {
                        return RedirectToAction("Index", "Roles", model);
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }

        //Below is my index method in my Roles controller which is the action that redirection needs to route to:

        [Route("/Roles")]
        public ActionResult Index()
        {
            if (TempData["StatusMessage"] != null)
            {
                ViewBag.StatusMessage = TempData["StatusMessage"].ToString();
            }
            else
            {
                ViewBag.StatusMessage = "";
            }

            var roles = db.Roles.ToList();
            return View("Index", roles);
        }



        //GET method to create view
        public ActionResult Create()
        {
          return View();
        }

View:

@model IEnumerable<User_Manager_Interface.Models.ApplicationRole>
@{

    Layout = "~/Views/Shared/_Layout.cshtml";
}


@if (ViewBag.StatusMessage != null)
{
    if (ViewBag.StatusMessage != "")
    {
        string tmp = ViewBag.StatusMessage;
        if (tmp.Contains("error"))
        {
            <div class="notification msgerror">
                <a class="close"></a>
                <p>@ViewBag.StatusMessage</p>
            </div>
        }
        else
        {
            <div class="notification msgsuccess">
                <a class="close"></a>
                <p>@ViewBag.StatusMessage</p>
            </div>
        }
    }
}

<br />
@Html.ActionLink("Create New", "Create", "Roles", new object { }, new { @class = "stdbtn" })
<br />
<br />

<div class="contenttitle radiusbottom0">
    <h2 class="table"><span>Roles</span></h2>
</div>

<table cellpadding="0" cellspacing="0" border="0" class="stdtable" id="dyntable">
    <colgroup>
        <col class="con0" />
        <col class="con1" />
        <col class="con0" />

    </colgroup>
    <thead>
        <tr>
            <th class="head1">Name</th>
            <th class="head0">Description</th>
            <th class="head1">Options</th>

        </tr>
    </thead>
    <tfoot>
        <tr>
            <th class="head1">Name</th>
            <th class="head0">Description</th>
            <th class="head1">Options</th>
        </tr>
    </tfoot>
    <tbody>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Description)
                </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>
        }
    </tbody>
</table>

@section Scripts {
    @Scripts.Render("~/bundles/tables")
}

Model:

    public class ApplicationRole : IdentityRole
    {
        [Display(Name = "Description")]
        [StringLength(100, MinimumLength = 5)]
        public string Description { get; set; }
    }

"Error" Controller:

    public class ErrorController : Controller
    {
        //
        // GET: /Error/
        public ActionResult Index(int statusCode, Exception exception)
        {
            Response.StatusCode = statusCode;
            return View();          
        }
    }

"Error" View:

@{
    ViewBag.Title = Response.StatusCode;
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2 style="visibility:hidden">@ViewBag.Title</h2>

<div class="errorWrapper">
    <h2>An internal server error @ViewBag.Title has occurred</h2>
    <h1 class="pageErrorTitle" style="color:red">Error @ViewBag.Title - Page Not Found</h1>
    <h3 style="color:black">You may have clicked an expired link or mistyped the address.</h3>
    <br />
    <a class="default" href="javascript:history.back()">Back to Previous Page</a> &nbsp; <a class="default" href="http://localhost:53648">Return to Dashboard</a>
</div>

Please see screenshots for more visual clarity:

Landing page to create a new role

enter image description here

After role has been added

JavaScript error associated with create new role landing page

Update: According to @Munzer 's answer below, I tried to change my Index action method to take in the model route value seeing as though I am passing it in my return statement. See below:

        public async Task<ActionResult> Create(ApplicationRole model)
        {

                if (ModelState.IsValid)
                {
                    var role = new ApplicationRole()
                    {
                        Name = model.Name,
                        Description = model.Description
                    };

                    var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(db));
                    var result = await roleManager.CreateAsync(role);
                    if (result.Succeeded)
                    {
                        //return RedirectToAction("Index", "Roles", model);
                        return RedirectToAction("SuccessfullyAddedNewRole", "Roles", model);
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }


            // If we got this far, something failed, redisplay form
            return View(model);
        }



       public ActionResult SuccessfullyAddedNewRole(ApplicationRole model)
       {
          return View(model);
       }

But the error still persists. I have tried everything I can think of.

Harold_Finch
  • 682
  • 2
  • 12
  • 33

2 Answers2

0

I think the error is here

return RedirectToAction("Index", "Roles", model);

you are passing route values but your index action doesn't accept any, it generate the value itself, you can simple do this instead

return RedirectToAction("Index", "Roles");,

and your index will query the new model, if you want to pass the model, you need to edit your index action accordingly, to accept role model.

Munzer
  • 2,216
  • 2
  • 18
  • 25
  • Hey Harold, can you go to network tab in chrome and click on the request and show the preview ? – Munzer May 29 '17 at 12:11
  • `POST http://localhost:53648/Roles/Create 500 (Internal Server Error)` Is the error I get – Harold_Finch May 29 '17 at 12:16
  • if you click on it, 5 tabs on the right will show, one of them is preview, click on it and show what it is showing I guess it will be the same, but just to double check. – Munzer May 29 '17 at 12:18
  • I can't see a preview option hey. I took a screenshot (documentation says Ctrl + G) to insert an image but no luck – Harold_Finch May 29 '17 at 12:32
  • then the last thing i might think of is, check your view name, is it matching the action name, and check the folder containing the view, is it the same name of the controller ? – Munzer May 29 '17 at 12:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145372/discussion-between-harold-finch-and-munzer). – Harold_Finch May 29 '17 at 12:52
0

Solution can be found on this SO post of mine. Fixed everything :-)

GET http://localhost/Roles/Create 500 (Internal Server Error)

Harold_Finch
  • 682
  • 2
  • 12
  • 33