I am using Rotativa (version 1.6.3) to generate pdf from my view. I have a simple partial view(_OverallResultPrintVersion.cshtml):
#Styles.Render("~/bundles/css")
<img src="#Url.Action("DrawChart", "Vote", new {area = "Award"})"/>
In my action when returning Rotativa PartialViewAsPdf, it opens an empty pdf page and the "DrawChart" action won't be called as expected.
Here is how I implemented My actions in Vote controller:
public ActionResult OverallResultPdf()
{
return new Rotativa.PartialViewAsPdf(
#"~\Areas\Award\Views\Shared\Widget\_OverallResultPrintVersion.cshtml");
}
public ActionResult DrawChart()
{
var model = getModel();
return PartialView("Widget/_VotesColumnChart", model);
}
When replacing the image source in partial view to an Url, it shows the image but this is not what I'm trying to achieve.
Any idea why Rotativa PartialViewAsPdf cannot call my action from partial view?
PS: there is no authorization restriction for these actions so I don't need to initiate FormsAuthenticationCookieName property when creating PartialViewAsPdf.
here is a workaround to resolve the issue. It costs adding a new Action! (OverallResultPrintVersion) and in OverallResultPdf action, instead of returning PartialViewAsPdf, an ActionAsPdf needs to be returned.
public ActionResult OverallResultPdf()
{
return new Rotativa.ActionAsPdf("OverallResultPrintVersion");
}
public ActionResult OverallResultPrintVersion()
{
return PartialView("Widget/_OverallResultPrintVersion");
}
and DrawChart() action remains untouched.
Related
Overview: I am currently attempting to build a create account form. The form is rendered on another razor page. All works correctly, the form displays, sends the form data to a controller, sends data to a class, performs all DB actions, but then upon the completion of the previous items , the program attempts to find a page "CreateAccount.something" when all I want it to do for the time being is to return the initial view upon the return call.
Within said project, a form is displayed via: #RenderPage("~/Views/Home/AccountCreationForm.cshtml")
The form:
#model SuperDuperProject.Models.AccountCreationModel // AccountCreationModel is only a class file containing the necessary variables
...
#using (Html.BeginForm("CreateAccount", "Home", FormMethod.Post))
{
<table cellpadding="0" cellspacing="0">
...
#Html.TextBoxFor(m => m.name)
...
<input type="submit" value="Submit"/>
</table>
}
The Controller file (HomeController.cs):
...
public ActionResult UserLogin() // the page containing the form
{
return View();
}
[HttpPost]
public ActionResult CreateAccount(AccountCreationModel ACM)
{
Console.WriteLine("CreateAccount within HomeController");
Helpers.CreateAccount a = new Helpers.CreateAccount(...);
a.AccountCreationQuery();
return Index(); // ********** Doesn't seem to operate correctly **********
}
Instead of returning Index() or anything placed there, the program attempts to find a CreateAccount view that does not exist.
What am I missing so I can simply return to a desired page, such as Index?
Any assistance would be greatly appreciated.
[HttpPost]
public ActionResult CreateAccount(AccountCreationModel ACM)
{
Console.WriteLine("CreateAccount within HomeController");
Helpers.CreateAccount a = new Helpers.CreateAccount(...);
a.AccountCreationQuery();
return RedirectToAction("Index");
// return Redirect("Home/Index"); alternatively can use Redirect
}
You could consider using RedirectToAction or Redirect.
RedirectToAction returns an HTTP 302 response to the browser, which causes the browser to make a GET request to the specified action. Redirect takes a string type URL parameter and redirects to that specified the URL.
Check out this post for more info:
https://www.codeproject.com/Articles/595024/Controllers-and-Actions-in-ASP-NET-MVC
Try this code
public ActionResult UserLogin() // the page containing the form
{
return View();
}
[HttpPost]
public ActionResult CreateAccount(AccountCreationModel ACM)
{
Console.WriteLine("CreateAccount within HomeController");
Helpers.CreateAccount a = new Helpers.CreateAccount(...);
a.AccountCreationQuery();
return View("Index"); //index is the view here. You can define the view //name which you want to return from this controller action
}
By default controller searches for the view name as the name of the controller action that's why it is attempting to locate the view CreateAccount because controller action name is CreateAccount.
Refered from Removing a "Print" button Before Rendering PDF from View - MVC3
Can anyone please provide the controller part of this answer??
Thanks in advance..
Merin
i dont think anyone can provide the controller but here is something that can help. You can install Rotativa for that purpose then create two actions like
public ActionResult Page(int id)
{
var data = //here you will get data from database by id
return View(data);
}
public ActionResult PrintPage(int Id)
{
return new ActionAsPdf(
"Page",
new { id= Id})
{ FileName = "Page.pdf" };
}
and also there will be view of name Page in which data will be populated when it will return the view of action Page it will be converted to pdf
Recently I've created on controller call DashboardVideos and an action method called Index.
And after Add Or Update, I'm redirecting it to Index page using
RedirectToAction("Index", "DashboardVideos").
but this code redirecting it to /DashboardVideos/ and it says
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.
so the issue is by default it's supposed to load Index page when I say /Dashboard
But its not, same url pattern working with all other controller (So I don't think there's anything wrong with routing pattern).
Any help would be appreciated.
Code:
public class DashboardVideosController : BaseController
{
private readonly IDashboardVideosComponent socialTagComponent;
public DashboardVideosController()
{
socialTagComponent = ComponentFactory.Get<IDashboardVideosComponent>();
}
// GET: DashboardVideos
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult AddUpdate(DashboardVideosModel socialTagChannel)
{
//Save data to database
return RedirectToAction("Index", "DashboardVideos");
}
}
Simply write this if both actions are in same controller.
public ActionResult AddUpdate(DashboardVideosModel socialTagChannel)
{
//Save data to database
return RedirectToAction("Index");
}
Try to take a look at your "RouteConfig" class and you can specify custom routes there. Also It is possible if the call comes from AJAX it can go directly to action without redirecting. Did you tried to Debug the code?
I'm faced with the following problem :
I have a controller with lets say the following actions:
[HttpGet]
public ActionResult Index()
{
var viewModel = new IndexViewModel();
return View("Index", viewModel);
}
[HttpPost]
public void ExportToExcell(LeadsViewModel model)
{
// Export to excell code goes here
}
The problem is the following:
The User enters on Index page with this URL : /Controller/Index
Then the user submits the form to Action ExportToExcel
Data is exported to Excel( file downloaded ) and it's okay.
The URL becomes /Controller/ExportToExcell
Then when I am clicking "Enter" I am going To /Controller/ExportToExcell but with GET
and of course falling with Page Not Found, the question is how properly to Deal with this in MVC
Don't use void as returned type of your post action, use an ActionResult
[HttpPost]
public ActionResult ExportToExcell(LeadsViewModel model)
{
// Export to excell code goes here
return RedirectToAction("Index");
}
I believe that your problem is that you aren't returning a FileResult, and the browser will redirect you to your post path. Can't test it right now, but I believe the following should work.
[HttpPost]
public ActionResult ExportToExcell(LeadsViewModel model)
{
// Generate the Excel file into a MemoryStream for example
// Return a FileResult with the Excel mime type
return File(fileStream, "application/vnd.ms-excel", "MyExcelFile.xls");
}
Check FileResult and Controller.File for more details.
As a note, I'm not completely sure if that's the mime type for an Excel file, but if you say you are already downloading the file, your probably already have it :)
You must return ActionResult instead of void.
public ActionResult ExportToExcel(PagingParams args)
{
var data = new StudentDataContext().Student.Take(200).ToList();
return data.GridExportToExcel<Student>("GridExcel.xlsx", ExcelVersion.Excel2007, args.ExportOption);
}
Please check the link: Export Action
I have a controller where all of the action methods contain the same code:
[ActionName("pretty-url")]
public ActionResult Something() {
return PartialView();
}
[ActionName("another-pretty-url")]
public ActionResult SomethingElse() {
return PartialView();
}
I name my partial views in the pretty-url.cshtml format, and these get picked up fine and everything works.
As every action in the controller will always do exactly the same thing and return the same thing, I would like to just have my controller look for the correctly-named view and return it as above, without me having to explicitly implement it.
How would I do that?
TIA
I would create a single action and pass the view name as parameter.
public ActionResult Something(string viewName)
{
return PartialView(viewName);
}
I would add a new method to my controller with a string parameter and use it to load the correct partial view.
public ActionResult Show(string PartialName)
{
return PartialView(PartialName);
}
Now instead of using http://your.domain/pretty_url you will have to use http://your.domain/show/pretty_url but this will work with any new partial view you add later on.