Search code examples
asp.net-mvcrazorasp.net-mvc-5viewbag

ViewBag value not showing up in POST Action of Controller?


I have a situation in my MVC5 App (Oracle Backend) wherein I must store the current URL when transferring to the GET of a View (this URL contains the User's previous navigated location as well as any/all sort/filter criteria set forth on the data-grid).

At first I thought I had figured this out by using a Session variable, Session["returnURL"], but while it functioned on my localhost, numerous attempts still lead to my session variable throwing a NullReferenceException during execution on my Production Server. I now have the following on my GET Edit action:

        if (Request.UrlReferrer.AbsoluteUri != null)
        {
            ViewBag.ReturnURL = Request.UrlReferrer.AbsoluteUri;
        }
        else
        {
            ViewBag.ReturnURL = null;
        }

If the above value is not null, I store it in the ViewBag to access on my View (specifying it as the href on an actionlink):

         @{
                if (ViewBag.ReturnURL == null)
                {
                    <span class="btn btn-default">
                        @Html.ActionLink("Back to List", "Index", "Home", new { @class = "btn btn-default" })
                    </span>
                }
                else
                {
                    <a href="@ViewBag.ReturnURL"><span class="btn btn-default">Back to List</span></a>
                }
            }
                     |
                    <input type="submit" value="Save"  class="btn btn-primary" />

My problem now is that when I attempt to Save changes to my record (thereby entering the POST of my Edit Action), I receive:

Server Error in '/' Application. Value cannot be null or empty. Parameter name: url. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentException: Value cannot be null or empty. Parameter name: url

The below is the relevant code of my POST Edit Action:

        if (ModelState.IsValid)
        {
            iNV_Assets.MODIFIED_DATE = DateTime.Now;
            iNV_Assets.MODIFIED_BY = System.Environment.UserName;

            db.Entry(my_Model).State = EntityState.Modified;
            await db.SaveChangesAsync();

            var returnURL = ViewBag.ReturnURL;
            return Redirect(returnURL);
        }

The record gets saved with whatever changes I make, but then the error is thrown when attempting to redirect back to the main View with the User's previous URL (and all specified search criteria).

Does anyone know why my ViewBag.ReturnURL is coming through as NULL on the POST Edit Action result? Any thoughts on how to achieve what I'm after with an alternative solution perhaps?


Solution

  • I ended up being able to use Session["variables"] for my Solution after all. I found the answer in this post (The Session object is null in ASP.NET MVC 4 webapplication once deployed to IIS 7 (W 2008 R2)), wherein I modified my <modules> section to do an add/remove of the below specified values:

      <configuration>
      ...
      <system.webServer>
        ...
        <modules>
          <remove name="Session" />
          <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
          ...
        </modules>
      </system.webServer>
    </configuration>
    

    I'm still not entirely sure WHY this is the answer, but it worked for my issue in this case.