Saving multiple images with size limitation and bootstrap - asp.net-mvc

I have built a simple ASP.NET MVC5 project that acts as a classifieds application for my college's students. I got everything figured out and implemented except for the images. I am struggling to find the best approach to store the images.
I came up with the following structure:
Ad represents the advertisement model
adImage represents an image(s) for an ad. AdImage has a foreign Key public int AdId { get; set; } and then public virtual Ad TheAdvertisement { get; set; }
Considerations:
I am using bootstrap
I will not consider support older IE browsers
I have to resize the images prior to save
I will be saving the images on the file system and then store their urls back into my database.
I think I know how to upload then store multiple images. I am struggling with how to implement size-limitation validation to my controller and whether it is something I should consider to implement in my project. What is the best approach when dealing with images in classified website?

To Limit the size of image files, take a look at file size upload limitation in ASP.NET MVC that employs web.config file to meet your purpose.
Another way to Limit file size is writing code something like this:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
//you can put your existing save code here
if (file != null && file.ContentLength > 10000)
{
//do whatever you want with the file
}
}
And the page File Upload ASP.NET MVC 3.0 contains good samples for you if you don't know how to write a controller to save files on file system.

Related

MVC Web form without database

I'm in the process of refitting a website that I'd previously built using ASP.net with VB in the code-behind, into MVC with VB so that it's more responsive to different screen sizes and device types.
So far I've been able to replicate six of the pages plus the Site.Master files. Now I'm turning my attention to the Contact page which in asp.net takes data from a form, validates it for completion and data-type compliance and then passes it to the code-behind which uses it to generate an email.
I've done quite a lot of reading which suggests using a Model but all the examples I've found then use that Model to populate or query a database using LINQ.
How can I do this without a database?
The M in MVC stands for Model, not Mdatabase. You can use whatever you want as the model. Most applications use a database and most .NET applications use EF to access a database (or at least Microsoft want it that way) but you can use whatever you want.
Using a database engine is recommended as permanent storage, but essentially you can create model class for contact page without involving a database like this:
public class Contact
{
[Required]
public String Name { get; set; }
[Required]
public String EmailAddress { get; set; }
}
And you can use LINQ/lambda expressions after wrapping your model class into a collection (e.g. List), as example:
var list = new List<Contact>();
list.Add(new Contact { ... }); // adding items to the list
// Lambda syntax
var name = list.Select(x => x.Name); // returns a value from the list
// LINQ syntax
var email = (from item in list
select item.EmailAddress);
Then you can convert the code-behind logic to a controller action method as ActionResult each for GET and POST request. Afterwards, Redirect, RedirectToAction or simply return View() after data submission can be used for page handling.
About responsive page design to work with different screen sizes and device types, MVC itself not designed to present responsive layouts by default. You need to alter some HTML & CSS-related attributes, as similar problem here:
How to make an MVC web application adjust to automatically fit different screen sizes
NB: Since your problem doesn't include any code, the provided codes are just examples as figure to what should be done.

User profile image asp.net mvc identity

I'm creating a web application using MVC 5 and Identity. I have so far created a registration system, but I would like to allow users to upload a profile when they register.
I was wondering if it is possible to implement profile pictures with asp.net identity?
Yes. Assuming you're using the default Entity Framework implementation, you can extend the ApplicationUser in a file called Models/IdentityModels.cs.
You can store the image in the database or elsewhere on the file system.
One way to store it in the database is using a byte array in the model (which I believe maps to varbinary(max))...
public class ApplicationUser : IdentityUser
{
public byte[] Image { get; set; }
}
Details on how to save upload an image and save in the database via EF can be found here...
Entity Framework 5 Code first adding an image
Or you can simply save the uploaded file to the file system or to blob storage, then store the path or URL to the image...
public class ApplicationUser : IdentityUser
{
public string ImagePath { get; set; }
}
Best thing todo is to create a second table (or add a column to the current table), add a file upload feature to the registration form. When registration is successful add the picture to the db with EntityFramework. Create a page to return the picture by the userid so you can include it in a page somewhere.
Something like this http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx but with a profile picture

Set file control value from model in asp.net mvc3 razor

In my MVC application, I am using following code for a file.
MODEL
public HttpPostedFileBase File { get; set; }
VIEW
#Html.TextBoxFor(m => m.File, new { type = "file" })
Everything working fine .. for submitting value But I am trying to load file from controller model which is not working
CONTROLLER
public ActionResult ManagePhotos(ManagePhoto model)
{
if(ModelState.IsValid)
{
//upload file
}
else
{
return View(model); //contains type HttpPostedFileBase File { get; set; }
}
}
how can i load file input again if my validation fails as after returning, my file control is not mapped to model to file and it's empty...
If you know that the file is valid, and want to keep it temporarily, you can keep it in session (but be aware of memory usage).
If you know the file is valid, and want to keep it permanently, save it and just keep the path in memory.
If you know that the file is invalid, you wont want to nor often be able to keep it.
Often this is treated in the same way during validation failure as a password - it needs to be provided again and as such is requested for only when everything else is good.
if you want to go as far as it takes to made user experience seamless, then try following:
Add javascript on the client side to validation the user inputs
On the server side temporarily store all the files on the tmp file storage. Clean up files older than 10 minutes. Do this when you need to save files into the storage

Best way for creating reports in ASP.NET MVC

After building my application in ASP.NET MVC and MS sql server, I would now like to display some statistics regarding my data.
What would be the easiest way to create HTML reports which are built of data crossing several tables? (Once the fields are picked they'll be static, meanning a single view is required)
I though their ought to be something in the lines of a wizard letting you drag fields from your tables to a form and generates the logic behind...
I wrote a blog post about this in September. It's a way to render a PDF content type using an RPT file in the application. It covers everything except the creation of the RDLC file, including how to write unit tests for the controller.
Microsoft Reporting Services?
"I though their ought to be something in the lines of a wizard letting you drag fields from your tables to a form and generates the logic behind..." - this is the basic idea behind ASP.NET WebForms. But, please do not abandon MVC in favour of WebForms.
One way to achieve what you want is to create a class representing your stats, e.g.
public class Statistic
{
public string TableName { get; set; }
public int RowCount { get; set; }
}
Your Model code could populate an IList<Statistic> instance which is passed to your View, which renders the stats accordingly.
You could take a look at Dynamic Data .... http://www.asp.net/dynamicdata

Reportviewer datasource in asp.net-mvc

How do I integrate ReportViewer in asp.net MVC project?
I want to add business objects of MVCProject.Model namespace. ReportViewer allows Business objects of DataSet.
Is it possible to choose other data source, like LinqDataSource, or Direct object to LINQ-to-SQL class objects?
What would be the best solution to add reports in an MVC project?
An alternative way to do this would be to generate the report on the reporting server, then stream it to the mvc app as a PDF.
I got an idea that is not tested but may work.
1- Place report viewer control in a standard ASP.Net web form page (e.g. ReportViewer.aspx)
2- Under your MVC, add an iframe that references to this ReportViewer.aspx page
3- Pass parameters to the page using sessions or query strings
Let me know if th is works
It's gonna be tough. First, you need ViewState so you'll need to host the report in a regular WebForms page. This isn't too bad though - WebForms and MVC work fine side-by-side.
The hard part is binding to real IEnumerable objects and not those phoney-baloney ObjectDataSources.
The first step is to build up a report data model. You can do this in code, with queries, whatever, however you want. A structure something like this (but obviously much bigger) is typical:
public class ReportSource
{
public Floogle[] Floogles { get; set; }
}
public class Floogle
{
public Doodad[] Doodads { get; set; }
public string Text { get; set; }
}
public class Doodad
{
public int Number { get; set; }
}
The trick is to use a BindingSource control in your report and set the DataSource property to typeof(ReportSource) - yes, the data source is the type of your report model.
When designing your report you won't get a lot of richness, but you'll be able to do it.
As far as third party reporting solutions go, we've found Telerik's to be the best option.
I've got a small project I threw up on codeplex that is an mvc project with a report.
http://mvctaskmanagement.codeplex.com/
Basically since I do dev on an XP box, my web form had to get pushed to a separate project. Since I have a service layer proj, I stuck it in there.
From there I call my report via a ajax post shooting the params over to the report page, which then passes it down to the same service layer used to generate the preview.
Good luck!

Resources