ASP.NET Export file to pdf - asp.net-core

I am newbie. I have 8 files cshtml and I want to combine all to 1 file (index.cshtml, I will export it to pdf). Can you help me? What should I write code to index.cshtml.
Thank for your help!
enter image description here

Hello Nguyễn Chí Linh,
If you know how to populate one viewmodel, you can combine these viewmodels into one like so:
public class viewModelGroup
{
public ViewModel1 viewModel1 { get; set; } = new ViewModel1();
public ViewModel2 viewModel2 { get; set; } = new ViewModel2();
}
For the output to PDF you can then use the nuget package manager to find many packages that will allow you to export the data to pdf. These packages come with their own documentation to show you how to convert your viewModelGroup into a PDF document.
Goodluck.

Related

How to save data into application vb.net ? (Not use database or txt file)

I a newbie in VB.net. I have a small project that save data from textbox. (just simple: create textbox, fill my name to textbox and save it).
But I don't want use database or txtfile because if I use like that, I will have 2 file: my app.exe and database file. I don't know whether there have anyway to save data into this my app.exe. Just have 1 file app.exe, database is included in this app (App is same to excel file: just have 1 file excel which can fill data and save)
Obviously you have a class Student (from your comment)
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
For binding to DataGridView you can use BindingList<Student>.
Then when you close your application save in JSON format.
var data = _yourBindingList.ToList();
var serializedData = Newtonsoft.Json.JsonConvert.SerializeObject(data);
System.IO.File.WriteAllText(#"fileName.anyExtensionYouWant", serializedData);
When application starts load data from the file and deserialize it.
var data = System.IO.File.ReadAllText(#"fileName.anyExtensionYouWant", serializedData);
var loadedStudents = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Student>>(data);
_yourBindingList = new BindingList(loadedStudents);
yourDataGridView.DataSource = _yourBindingList;
The easy way is to save the text to a txt file
My.Computer.FileSystem.WriteAllText
https://msdn.microsoft.com/es-es/library/27t17sxs(v=vs.90).aspx
So
My.Computer.FileSystem.WriteAllText("C:\TestFolder1\test.txt", "This is new text to be added.", False)
on your're case can be
My.Computer.FileSystem.WriteAllText("C:\TestFolder1\test.txt", textbox1.text, False)
If you need to use MyDocuments or Desktop folders you need to use relative system routes

Sonar Custom Widget

I created a widget using the source code available in github. Now I'm using that widget in SonarQube V5.3. This is where I got the source code from:
https://github.com/SonarSource/sonar-examples/tree/master/plugins/sonar-reference-plugin
When I use this widget it is showing up the same data across multiple projects. I would like to know if there is any way I can display different data for different projects. Please share your ideas. Below is the code that displays the ruby widget
import org.sonar.api.web.AbstractRubyTemplate;
import org.sonar.api.web.Description;
import org.sonar.api.web.RubyRailsWidget;
import org.sonar.api.web.UserRole;
import org.sonar.api.web.WidgetCategory;
import org.sonar.api.web.WidgetProperties;
import org.sonar.api.web.WidgetProperty;
import org.sonar.api.web.WidgetPropertyType;
import org.sonar.api.batch.CheckProject;
import org.sonar.api.resources.Project;
#UserRole(UserRole.USER)
#Description("Sample")
#WidgetCategory("Sample")
#WidgetProperties({
#WidgetProperty(key = "Index",type=WidgetPropertyType.TEXT
),
})
public class OneMoreRubyWidget extends AbstractRubyTemplate implements RubyRailsWidget {
#Override
public String getId() {
return "Sample";
}
#Override
public String getTitle() {
return "Sample";
}
#Override
protected String getTemplatePath() {
return "/example/Index.html.erb";
}
}
Thank you so much in advance
You haven't specified global scope for your widget (#WidgetScope("GLOBAL")) in the .java file, so this is a question of what's in your .erb file.
This Widget Lab property widget should give you some pointers. Specifically: you want to pick up #project in your widget, and query with #project.uuid. Here's another project-level widget for comparison.
You should be aware, though, that SonarSource is actively working to remove Ruby from the platform, so at some future date, you'll probably end up re-writing your widgets (likely in pure JavaScript).

Submit data along with File Upload

I am trying to post form data (such as textbox, checkbox etc) along with a File Upload . I am using MVC.
Any one give me a solutions?
There are so many ways to do it with MVC, with strong-typed as suggested above or this way also would work
[HttpPost]
public JsonResult CreateUpdate(FormCollection _formValues, YourModel _extraItem)
{
HttpPostedFileBase files = HttpContext.Request.Files;
//do whatever u wish with ure files here
}
hope it helps
Your Post Controller will look like this :
[HttpPost]
public ActionResult YourController(YourModel model1, HttpPostedFileBase file)
{
if (file != null)
{
//here file variable will have the file which you have uploaded
}
}
HttpPostedFileBase contains the file which you have posted from View.
and in your view BeginForm() should look like :
Html.BeginForm(action, controller, FormMethod.Post, new { enctype="multipart/form-data"})
i use XMLHttpRequest to resolve this solution. Thanks everyone so much :D

convert string to array in mvc 4

In my recent mvc 4 project, I store multiple image and other files (such as doc, pdf, ppt etc.) as string in my database. Now I want to show this multiple image and want to show link of other files.
For example, I store data as string in my db as like as given below:
1980082_10201802177236118_516383197_o.jpg, ASP.NET MVC Interview Questions &amp; Answers.pdf, Sample-1.jpg,
Now I want to fetch this string and show image and give the link of the other files.
I hope it will help.First when you save in your database you have to make sure you save it with all the paths, not only the filenames. In simple In your Action:
public FilePathResult GetDoc(int yourParam)
{
var path= (from r in yourContext.yourTable where id == yourParam
select r.path).FirstOrDefault()
//Considering that your path contains also the folder
return new FilePathResult(path);
}
In your View:
<img src="#Url.Action("GetDoc", "YourController", new { yourParam=Model.yourParam}) " />
#*For a link*#
<a href="#Url.Action("GetDoc", "YourController", new { yourParam=Model.yourParam}) ">#Model.TheNameOfYourFile <a/>
Firstly note that, it is not good idea to store that file names in one row in db. If I understood you right, to extract images and files from that rows, create a model that contains list of different file list:
public class RowResult
{
public RowResult()
{
Images = new List<string>();
Pdfs = new List<string>();
}
public List<string> Images { get; set; }
public List<string> Pdfs { get; set; }
//you can add other file types here
}
Create a method that return result for every row:
public RowResult ExtractFilesFromRow(string row)
{
RowResult result = new RowResult();
string[] parts = row.Split(' ');
foreach (string part in parts)
{
if (part.TrimEnd(',').EndsWith(".jpeg")) result.Images.Add(part);
if (part.TrimEnd(',').EndsWith(".pdf")) result.Pdfs.Add(part);
//add others here..
}
return result;
}
And finally, to show them in view for each row you can get RowResult in db and fill List<RowResult>. In Action:
....
List<RowResult> list = new List<RowResult>();
foreach (string row in rows)
{
list.Add(ExtractFilesFromRow(row));
}
...
return list;
In view:
#foreach (string result in Model)
{
//here depends on you want
<img src="#Url.Content("~/Uploads/Images/" + result.Images[0])" />
....
<img src="#Url.Content("~/Uploads/Images/" + result.Images[1])" />
}
I store multiple image and other files (such as doc, pdf, ppt etc.) as string in my database
I assume you mean you save the file names as string in your database.
Now let's start with the db data you shared
For example, I store data as string in my db as like as given below:
1980082_10201802177236118_516383197_o.jpg, ASP.NET MVC Interview Questions & Answers.pdf, Sample-1.jpg,
when comma seperated becomes this
1980082_10201802177236118_516383197_o.jpg
ASP.NET MVC Interview Questions & Answers.pdf
Sample-1.jpg
Now what we have here is only filenames and to "physical path" to where these files are stored.
So for this to work you WILL have to save the file path in the database too. Unless you have defined a path say "C:/SomeApp/MyDump/" and you are simply dumping all the files here.
In case you are then you can use that path append it with the file name and show on the UI. The file extension could help you decide what HTML to use (<img> or <a> to display image or show a download link)

Unable to evaluate expression in PCLStorage

I'm using PCLStorage in my Win8/Wp8 apps, but for simplicity I'll direct my question only for Win8.
I've referenced the PCLStorage package in my Win8 project and also a PCL project. In my PCL project, I'm creating a document management class to handle the saving/retrieving of files.
private IFileSystem LocalFileSystem { get; set; }
protected IFolder RootFolder { get; set; }
protected IFolder RoamingFolder { get; set; }
public DocStorageManager()
{
LocalFileSystem = FileSystem.Current;
RootFolder = LocalFileSystem.LocalStorage;
RoamingFolder = LocalFileSystem.RoamingStorage;
}
When debugging I see that LocalFileSystem gets set to an instance of PCLStorage.WinRTFileSystem. RootFolder and RoamingFolder however show up as Could not evaluate expression.
Now if I go into the Immediate Window and type FileSystem.Current.LocalStorage I get a valid WinRTFolder with a path to my local storage:
FileSystem.Current.LocalStorage
Name = "LocalState"
[PCLStorage.WinRTFolder]: Name = "LocalState"
Name: "LocalState"
Path: "C:\\Users\\myuser\\AppData\\Local\\Packages\\1957d424-34d5-4856-9186-4a64dfaf1ae7_q5jzzq53vdxma\\LocalState"
However I cannot simply assign RootFolder = FileSystem.Current.LocalStorage because it then says Could not evaluate expression again.
It sounds like there is a fundamental behavior I don't understand. How should I structure this to use it as I need to?
many thanks!