Issues like this are why I'm more and more considering using a BaseViewModel class to inherit all of my view models from to store information that may be useful to have on any given page. I've been trying to come up with a complete class definition lately, but for these purposes it would probably be nice to have the follwing properties (posted part of this in an answer to a different question, I'll find a link in a bit):
public class BaseModel
{
public string PageTitle { get; set; }
public string PageDescription { get; set; }
public string PageKeywords { get; set; } //maybe use a List<string> or string[] here
public string ReturnPage { get; set; }
//TBD: any other useful HTML page elements
}
Then I could create a view model that inherits from this:
public class RandomViewModel : BaseViewModel
{
RandomViewModel()
{
//set page properties
}
}
Then in a service layer (preferably) or in a controller (may have to do it there, haven't tried this so can't be sure) you would have access to the ReturnPage property when constructing your model (in this example I'll assume you're doing this in the action):
public ActionResult RandomAction()
{
RandomViewModel model = _serviceLayer.GetRandomViewModel();
model.ReturnPage = this.HttpContext.Request.UrlReferrer;
return View(model);
}
Then in the view you would be able to do this:
<input type="button" value="Cancel" onclick="if (history.length == 0) { window.location='<%= Model.ReturnPage %>' } else { history.back() }" />
This is all just kind of brainstorming, but I think it would work. The only big issue I'm not sure of is where you would want to set the referrer value. I would also try and get that onclick event out of the element and set it in the header if you can.
Hope this helps.