2017-05-26 24 views
0

再次,經過無數嘗試解決這個煩人的問題後,我回到了這裏。在我的控制器上執行了一些return RedirectToAction("Index", "Roles", model);動作後,我在我的MVC5應用程序視圖中遇到了「對象移到此處」。對象移動到這裏 - RedirectToAction

我已經考慮從以下幾個環節的解決方案:

  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 但都不適用於我的問題。有人標誌着其視爲重複

之前,請考慮以下情況:

但現在奇怪的是,我點擊後的「這裏」鏈接,它現在呈現我視圖 - 它有一個水平分割屏幕,顯示我的視圖的頂部和下部的正確顯示,引發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的答案,我想改變我的Index操作方法取模型路線值看到,因爲雖然我通過它在我的回報聲明。見下:

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

但是錯誤仍然存​​在。我嘗試了所有我能想到的。

回答

0

我認爲錯誤是在這裏

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

您傳遞路線的值,但是你的索引操作不接受任何,它產生的價值本身,你可以簡單的做到這一點,而不是

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

並且您的索引將查詢新模型,如果您要傳遞該模型,則需要相應地編輯您的索引操作,以接受角色模型。

+0

請參閱更新。 –

+0

嘿哈羅德,你可以去鉻網絡選項卡,並點擊請求,並顯示預覽? – Munzer

+0

'POST http:// localhost:53648/Roles/Create 500(內部服務器錯誤)'我得到的錯誤是 –