Object Moved Here - RedirectToAction

情到浓时终转凉″ 提交于 2020-01-17 06:53:18

问题


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:

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.


回答1:


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.




回答2:


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

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



来源:https://stackoverflow.com/questions/44197847/object-moved-here-redirecttoaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!