对象已移至此处 - RedirectToAction

时间:2017-05-26 09:13:56

标签: javascript c# asp.net asp.net-mvc razor

再次,在经过无聊的尝试解决这个恼人的问题之后,我又回到了这里。在我的控制器上执行了return RedirectToAction("Index", "Roles", model);操作后,我在MVC5应用程序中的视图中遇到了“对象移动到此处”。

我考虑过以下链接中的解决方案:

  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 但没有一个适用于我的问题。
  4. 之前有人将此标记为重复内容,请考虑以下方案:

    但现在奇怪的是,点击“此处”链接后,它现在呈现我的视图 - 它有一个水平分屏,显示我的视图在顶部和在抛出Http错误代码的视图下方 - 在这种情况下,其错误代码500:内部服务器错误

    我收到http状态代码错误的原因是由于这个未解决的问题(许多人中的一个):

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

    但这就是现在的重点。

    控制器:

            [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();
            }
    

    查看:

    @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")
    }
    

    型号:

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

    “错误”控制器:

        public class ErrorController : Controller
        {
            //
            // GET: /Error/
            public ActionResult Index(int statusCode, Exception exception)
            {
                Response.StatusCode = statusCode;
                return 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>
    

    请查看截图以获得更清晰的视觉效果:

    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

    更新 根据@Munzer在下面的回答,我尝试更改我的索引操作方法以获取模型路由值,就像我在return语句中传递它一样。见下文:

            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);
           }
    

    但错误仍然存​​在。我已经尝试了我能想到的一切。

2 个答案:

答案 0 :(得分:0)

我认为错误就在这里

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

您正在传递路线值,但您的索引操作不接受任何,它会自动生成值,您可以简单地执行此操作

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

并且您的索引将查询新模型,如果要传递模型,则需要相应地编辑索引操作,以接受角色模型。

答案 1 :(得分:0)

解决方案可以在我的SO帖子上找到。修正了一切:-)

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

相关问题