Rotativa --header-html using MVC view is not working - rotativa

I have recently started using Rotativa in my project. I have HTML page which will list out data in multiple tables.I need to display page number as footer which is working and display header using MVC view when user view this HTML as PDF. Problem that I'm facing is I dont see header being displayed. I can access the header URL standalone and it loads fine but unable to load it within pdf. Can anyone please help me here?
Here are my action methods:
Below action method will render PDF:
public ActionResult PrintBenificiaryForm()
{
PrintBenificiary p = new PrintBenificiary();
p.TeamMemberInfo = GetSessionValues();
p.IndividualBenefitPlans = GetBenefitPlansandIndividuals();
string customSwitches = string.Format(" --footer-left \"[page] of [toPage]\" --footer-spacing -10 --footer-font-size \"10\" --header-html \"{0}\" ", Url.Action("PrintHeader", "Home", null, this.Request.Url.Scheme));
return new ViewAsPdf("PrintBenificiaryForm", p)// { FileName = p.TeamMemberInfo.FirstName + " " + p.TeamMemberInfo.LastName + ".pdf" };
{
CustomSwitches = customSwitches
};
//return View(p);
}
Action method for header
[AllowAnonymous]
public ActionResult PrintHeader()
{
TeamMemberInfoModel t= GetSessionValues();
return View(t);
}
I have attached my view details and location of header file.
Also I observed that when I pass third parameter in URL.action as new {area=""} as I get blank on PDF.
null returned when I passed area as empty to URL.Action method
But when I tried passing null , I get 401.2 unauthorized error.
This is URL returned when I passed to URL.Action method -http://localhost:3842/p/Home/PrintHeader

Related

Redirect after AJAX form submission

Using MVC 4, I have a partial view form which contains an #Ajax.BeginForm.
The form submits as expected, and the result is displayed asynchronously in my main view.
I want a condition on my controller that if a certain parameter is true on my form, then it redirects to a whole new page (instead of displaying the result in my main view).
When I tried return RedirectToAction, the whole view displays in the div that the form normally displays in, as opposed to ignoring the AJAX and redirecting to a completely new page.
Does anyone know how I can acheive this?
You can use return JavaScript to achieve it.
public ActionResult MyAction()
{
if (parameter)
{
return JavaScript("window.location = '" + Url.Action("Action", "Controller") + "'");
}
//Do something here
return PartialView("ParitalView", Model);
}
You could not perform RedirectToAction in a ajax call.
Just return a HttpStatusCodeResult and based on it perform redirect in Javascript
public ActionResult Save()
{
return new HttpStatusCodeResult(302,
"/Users/Details");
}
In Ajax Error function, set in AjaxOptions { OnFailure = "Error" }
function Error(response, status, error) {
window.location.href= response.statusText;
}

Make link Ajax.ActionLink in MVC 4

Beeing new to Ajax i need help with how to add the querystring and invoke the Index action without a postback. Now the link looks like this:
#sectionGroup.Term, #sectionGroup.Count
My guess is using Ajax.ActionLink but how do i create the querystring?
Everything that you add to the routeValues and doesn't match a route segment will be added to the querystring.
Using your example, it would look something like this with Ajax.ActionLink:
#Ajax.ActionLink(
sectionGroup.Term + ", " + sectionGroup.Count,
"Index",
new { section = sectionGroup.Term },
new AjaxOptions { UpdateTargetId = "id-of-container" }
)
In your Index action you need to return a partial view when it is requested with ajax. This will remove the layout (master page) from the response.
if (Request.IsAjaxRequest())
return PartialView(model);
return View(model);
Remember to add a reference to the jquery.unobtrusive-ajax.js file.

MVC3 Dynamic Return URL

I want to have a View that has a link that will be set to the url of whatever page the user navigated to this view from.
Let's say I have a View and relative Action named InfoPage, and on this page I want a link that simply says 'Return'.
If the user is on PageA and navigates to InfoPage, clicking the 'Return' link returns the user to PageA.
If the user is on PageB and navigates to InfoPage, clicking the 'Return' link returns the user to PageB.
I'm thinking the easiest means to do this will be to add the 'ReturnUrl' as a property of the model used in InfoPage.
My question this is how do I get that return url.
public ViewResult InfoPage(){
var model = new InfoPageModel();
//Set my model's other properties here...
model.ReturnUrl = ''//Where do I get this?
return view(model);
}
And then in my view
Return
The most robust way to do this is to pass a query-string parameter to your page from the caller page. Every link to this page will be required to pass its own URL. (Request.Url).
You could also use Request.UrlReferrer in your control, but not all browsers send Referer headers.
To dynamically construct a returnUrl within any controller action:
var formCollection =
new FormCollection
{
new FormCollection(this.HttpContext.Request.Form),
new FormCollection(this.HttpContext.Request.QueryString)
};
var parameters = new RouteValueDictionary();
formCollection.AllKeys
.Select(k => new KeyValuePair<string, string>(k, formCollection[k])).ToList()
.ForEach(p => parameters.Add(p.Key, p.Value));
var returnUrl =
this.Url.Action(
this.RouteData.Values["action"].ToString(),
this.RouteData.Values["controller"].ToString(),
parameters
);
Related: How do I redirect to the previous action in ASP.NET MVC? (Same but from within any View)

How do you perform a 302 redirect using MVC3 and ASP.Net?

I'm using Razor, HTML5, MVC3 with C# in an application, where after a user clicks on a link, I open a new window, do some processing, then want to redirect that window with a 302 status code to a link.
Thanks.
The correct way to do this in ASP.NET MVC is by having a controller action that returns a redirect ActionResult. So inside the controller action you are invoking in this window and doing the processing simply perform a redirect by returning a proper ActionResult:
public ActionResult Foo()
{
// ... some processing
return RedirectToAction("SomeAction", "SomeController");
}
When the Foo action is invoked (presumably inside the new window) it will do the processing and return a 302 HTTP status code to the client with a new location of /SomeController/SomeAction.
If you wanted to redirect to some external url of your application you could do the following:
public ActionResult Foo()
{
// ... some processing
return Redirect("http://someotherdomain.com/somescript");
}
As far as creating a link that will open in a new window/tab is concerned you could append the target="_blank" attribute on the anchor:
#Html.ActionLink(
"Some link", // linkText
"Foo", // action
"SomeController", // controller
null, // routeValues
new { target = "_blank" } // htmlAttributes
)

How to load an image from ASP.NET MVC controller in a Facebook canvas app?

In my Facebook canvas application, I want to load an image from my ASP.NET MVC Controller.
So I do somethinhg like this, in my controller (simplified):
public ActionResult GetImage(int id)
{
// ....
return File(#"c:\temp\1.jpg", "image/jpeg");
}
And it's called from my aspx page like so:
<img src="/Home/GetImage?id=20" alt="Test"/>
But the image is not displayed, I get a "red x".
So I fire up Fiddler and see that the request does indeed return the image, but the data is prepadded with Facebook's frame html:
<html><head><script type="text/javascript">
top.location = "http://www.facebook.com/dialog/oauth/?state=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&client_id=xxxxxxxxxxxxxxx&redirect_uri=http://localhost:5000/facebookredirect.axd";
</script></head><body></body></html>?????JFIF??H?H????
?ICC_PROFILE???
????????mntrRGB XYZ ?????$?acsp???????????????????????????????????
Which of course is not vaild jpeg image data.
How can I avoid this pre padded data?
Never mind, I found out myself that the signed_request data was (yet again) missing.
I thought the only way to pass this was thru a HTTP header, but (like magic) I could pass it as a parameter as well causing the request to be validated.
<img src="/Home/GetImage?id=20&signed_request=<%=Request.Params["signed_request"] %>" />
public ActionResult GetImage(int id), string signed_request)
{
var auth = new CanvasAuthorizer();
if (auth.Authorize())
{
// Indeed, I'm now authorized
return File(#"c:\temp\1.jpg", "image/jpeg");
}
/// ...
}
/M

Resources