Removing a “Print” button Before Rendering PDF from View - MVC3 - asp.net-mvc

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

Related

mvc action not being called from Rotativa PartialViewAsPdf

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.

How to get view name in controller while navigating from view to controller in MVC3

I have below view in my project,
PolicyScreen.cshtml
The above view has below control,
#Html.ActionLink("OK", "AccountScreen", "AccountUC", new { id= ViewBag.id})
Account controller looks like below,
[ActionName("AccountScreen")]
public ActionResult GetPolicyController(int id)
{
if (viewName='PolicyScreen')
{
//do validation
}
}
If I click OK, I am able to hit AccountUC controller and AccountScreen Action Name properly.
Now I need to know from which view I was navigated to AccountUC controller?
The answer is PolicyScreen, but I don't know how to get this view name in action method,any help?
I wonder why you need this, but you can do this by putting the view name in the action link parameters:
new { id = ViewBag.id, fromView = "PolicyScreen" }
And of course you'll need to alter your action method's signature:
public ActionResult AccountScreen(int id, string fromView)
If you want to get the view name automatically rather than hardcode it, see Retrieve the current view name in ASP.NET MVC?.
If you want the action name rather than the view name, see Get current action and controller and use it as a variable in an Html.ActionLink?.
Try this
#Html.ActionLink("OK", "AccountScreen", "AccountUC",
new { id= ViewBag.id , viewName="replaceCurrentViewNameHere"},null)
Make sure your action method has a parameter to accept the viewname we are sending
[ActionName("AccountScreen")]
public ActionResult GetPolicyController(int id,string viewName)
{
if (viewName=='PolicyScreen')
{
//do validation
}
}

Getting List values in ActionResult Controller

I am learning mvc 4. Im new to mvc and i had implement ajax call for passing parameters and
I have one List shown in below:
[HttpPost]
public List<Reportdata> ReportTransaction(string dfromdate, string dtodate, int stype)
{
List<Reportdata> reportdataobject = new List<Reportdata>();
System.Globalization.CultureInfo format = System.Globalization.CultureInfo.CurrentCulture;
var linq = (from db in Invmsobject.sp_procReports(DateTime.ParseExact(dfromdate, "MM/dd/yyyy", format), DateTime.ParseExact(dtodate, "MM/dd/yyyy", format), ((stype == 1) ? "In" : "Out").ToString())
select new Reportdata
{
ITransactionDate=db.TransactionDate.ToString(),
IProductName=db.ProductName,
ITransactionType=db.TransactionType,
IQty=db.Qty,
IUserName=db.UserName
}).ToList();
return linq;
}
Controller
public ActionResult Report()
{
return View();
}
I dont know how to assign this list values to this Report Controller.
I can assign by using this List name[ReportTransaction] but this List having Parameters that why i'm getting struck on this... I had use ajax call for passing parameters in this list.. This List working fine but i could not able to get this List datas to Controller. can anyone help me how to handle this kind of situations..
Thank you.
If this isn't what you are asking, please clarify what you are trying to do, as it is unclear.
Controller:
public ActionResult Report()
{
return View(ReportTransaction(...));
}
View:
#model List<Reportdata>
<h2>Details</h2>
#foreach(var item in Model)
{
<div>#item.IProductName</div>
}

MVC Routing Issue - refreshing same page

I have an Edit page and once the form is submitted I'm refreshing the page instead of redirecting the user to the Index page. To do so I'm saving the ID of the item in a temp variable and then use it to redirect the user to the edit page using the temp variable ID. Something like this:
[HttpGet]
public ActionResult Edit(Guid id)
{
TempData["CategoryID"] = id;
Category c = new CategoriesBL().GetCategory(id);
return View(c);
}
[HttpPost]
public ActionResult Edit(Category c)
{
new CategoriesBL().UpdateCategory(c);
return RedirectToAction("Edit", (Guid)TempData["CategoryID"]);
}
That's working fine. However I have two methods in a different form on the same page and whenever I submit either of these two methods the redirection is not working and I'm getting an exception.
One of the methods that's not working:
[HttpPost]
public ActionResult AddNewThumbnail()
{
List<byte[]> thumbs = new List<byte[]>();
for (int i = 0; i < Request.Files.Count; i++)
{
thumbs.Add(ConvertToByteArray(Request.Files[i].InputStream));
}
new CategoriesBL().AddCategoryThumbnail(thumbs, (Guid)TempData["CategoryID"]);
return RedirectToAction("Edit", (Guid)TempData["CategoryID"]);
}
Exception:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Guid'....
I think it's an issue with routing but the fact is that the same implementation is used and it's working on one form and not the other. I'm not sure whether I'm doing something wrong or if there's any better way to do this.
Note: I have debugged the code several times and the ID I'm passing to the method does have a value in it. However when the page reloads the URL has no ID present.
Debugging
The problem seems to be due to the different forms I'm using. The first form I'm just editing text and it is like so:
#using (Html.BeginForm()) {
// ....
}
In the second form I'm saving and uploading images so the form has to be different
#using (Html.BeginForm("AddNewThumbnail", "Category", FormMethod.Post, new { enctype = "multipart/form-data" })) {
// ....
}
Somehow when I changed the form to the 'normal' one everything worked. But of course I can't use it as I want to save images from this form.
pass the value from your view. Something like this
[HttpPost]
public ActionResult Edit(Category c, FormCollection f)
{
Guid categoryID = (Guid)f["catergoryID"];
new CategoriesBL().UpdateCategory(c);
return RedirectToAction("Edit", catergoryID);
}
In your first example, you have initialisation:
TempData["CategoryID"] = id;
in GET method. So, you have to init your (Guid)TempData["CategoryID"] before you try to access it here:
return RedirectToAction("Edit", (Guid)TempData["CategoryID"]);

How to return a View in a different controller

Class Student {
String Name {get; set}
//Extended property of the student
School Shooling {get; set;}
}
public StudentControlelr ()
{
public SchoolInfo (int? ID)
{
Student s = StudentRepository.GetStudentByID(id)
Return View ("Schooling/Index", s.Schooling);
}
}
for some reason i have to make this view as a shared view
// Views/Shared/schooling.ascx
Return View ("Schooling", s.Schooling);
This works but why not if its in a different view folder it won't work? Am I missing some thing here?
Please note that ASP.net novice.
Kind Regards
Vinay
You can redirect to the action:
return RedirectToAction("Action", "Controller");
The only issue here is that you cannot pass a model, but there are a couple ways to get around this:
Pass parameter and get the model upon load
return RedirectToAction("Action", "Controller", new { id = 1});
Pass the model using TempData
TempData["MyModel"] = s.Schooling;
return RedirectToAction("Action", "Controller");
This will work but a better solution would be to redirect to a different action:
return View ("~/Views/Schooling/Index.aspx", s.Schooling);
Note: You will need to change .aspx to your views extension if you are not using ASPX Views.

Resources