MVC file saving and uploading with same button - asp.net-mvc

I have a Upload and Save forms that works but I would like to pass uploaded image and save other data for movies in one button (to merge Save and Upload)
Here is my cshtml Create view:
#model MvcMovie.Models.Movie
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Movie</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Title)
#Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ReleaseDate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ReleaseDate)
#Html.ValidationMessageFor(model => model.ReleaseDate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Genre)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Genre)
#Html.ValidationMessageFor(model => model.Genre)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Price)
#Html.ValidationMessageFor(model => model.Price)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Rating)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Rating)
#Html.ValidationMessageFor(model => model.Rating)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ImageUrl)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ImageUrl)
#Html.ValidationMessageFor(model => model.ImageUrl)
</div>
<input type="submit" value="Create" />
</fieldset>
<img src="~/Content/Images/Full/image1.JPG" alt="Sample Image" width="300px" height="200px" />
}
#using (Html.BeginForm("Upload", "Movies", FormMethod.Post, new { enctype="multipart/form-data" }))
{
<input name="ImageUploaded" type="file">
<input type="submit" value="Upload"/>
}
#Html.ActionLink("Back to List", "Index")
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
Here's my Controller:
public ActionResult Create()
{
return View();
}
//
// POST: /Movies/Create
[HttpPost]
public ActionResult Create(Movie movie)
{
if (ModelState.IsValid)
{
db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index", "Movies");
}
return View(movie);
}
public ActionResult Edit(int id = 0)
{
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
[HttpPost]
public ActionResult Upload(ImageModel model)
{
if (ModelState.IsValid)
{
string fileName = model.ImageUploaded.FileName;
string serverPath = Server.MapPath("~");
string imagesPath = serverPath + "Content\\Images\\";
String fullPath = #"" + "C:/Users/FAZI-7/Desktop/Full/";
String thumbPath = #"" + "C:/Users/FAZI-7/Desktop/Thumb/";
ImageModel.ResizeAndSave(thumbPath, fileName, model.ImageUploaded.InputStream, 80, false);
ImageModel.ResizeAndSave(fullPath, fileName, model.ImageUploaded.InputStream, 600, false);
}
return RedirectToAction("Index", "Movies");
}
I can't make one button for both functions, I have read that I might need to have the same controller action having two parameters, or put all the controls in one form, but how would I do this?

Here's a working example. I think one of the critical pieces you're missing is the enctype parameter on your HTML form element.
Create.shtml
#model ARES.Models.EmployeeViewModel
#{
ViewBag.Title = "Create New Employee";
}
#using (Html.BeginForm("Create", "Employees", FormMethod.Post, new { enctype = "multipart/form-data" })) {
<fieldset class="containedAndCentered">
<div class="editor-label">
Department
</div>
<div class="editor-field">
#Html.DropDownListFor(m => m.SelectedDepartmentId, new SelectList(Model.Departments, "department_id", "department_name", Model.SelectedDepartmentId))
#Html.ValidationMessageFor(model => model.Departments)
</div>
<div class="editor-label">
Name
</div>
<div class="editor-field">
#Html.EditorFor(model => model.EmployeeName)
#Html.ValidationMessageFor(model => model.EmployeeName)
</div>
<div class="editor-label">
Photo
</div>
<div class="editor-field">
<input type="file" id="EmployeePhoto" name="EmployeePhoto" />
</div>
<div class="editor-field">
#Html.HiddenFor(model => model.EmployeeId)
</div>
<hr />
<div class="controls left">
#Html.ActionLink("Back to List", "Index")
</div>
<div class="controls right">
<input type="submit" value="Create" />
</div>
</fieldset>
}
EmployeesController.cs
namespace ARES.Controllers {
public class EmployeesController : Controller {
private employee_picsEntities db = new employee_picsEntities();
[HttpPost]
public ActionResult Create(EmployeeViewModel evm) {
if (ModelState.IsValid) {
// Find the selected department object.
var department = (from d in db.departments
where d.department_id == evm.SelectedDepartmentId
select d).FirstOrDefault();
employee_master employee_master = new employee_master() {
department = department,
employee_name = evm.EmployeeName,
file_location = evm.EmployeePictureUrl,
status = 1
};
// Add the employee to the collection before we set the file_location field so that we have the neccessary employee_id value.
db.employee_master.Add(employee_master);
db.SaveChanges();
// Save a photo if we were provided with one.
if (evm.EmployeePhoto != null) {
employee_master.file_location = SaveEmployeePhoto(employee_master.employee_id, evm.EmployeePhoto);
}
//db.employee_master.Add(employee_master);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(evm);
}
public string SaveEmployeePhoto(int employeeId, HttpPostedFileBase employeePhoto) {
var basePath = "~/Content/images/photos";
var filename = "";
var path = Server.MapPath(basePath);
var savedFileName = "";
HttpPostedFileBase file = Request.Files[0];
if (file.ContentLength > 0) {
filename = employeeId + Path.GetExtension(file.FileName);
savedFileName = Path.Combine(path, filename);
file.SaveAs(savedFileName);
}
//filename = (basePath + "/" + fileName).ToAbsoluteUrl()
return string.Format("{0}/{1}", basePath, filename).ToAbsoluteUrl();
}
}
}

Related

How to retain formcollection values on postback in MVC

Here I am using simple application and on clicking send button I will execute Index httppost action. But for some reason if my captcha is not correct then i am trying to load the same view. I am passing form collection so my firstname and lastname are not wiped out. Please suggest how can i persist my values.
[HttpPost]
public ActionResult Index(FormCollection dataColl)
{
ColComments datgrp = new ColComments();
datgrp.fname = dataColl[0].ToString();
datgrp.lname = dataColl[1].ToString();
if (!this.IsCaptchaValid(""))
{
ViewBag.Classname = "alert alert-warning";
ViewBag.ErrorMessage = "Incorrect captcha answer.";
}
else
{
ViewBag.ErrorMessage = "OKAY";
return RedirectToAction("Landing", "Account");
}
return View(dataColl);
}
Index view.
#using CaptchaMvc.HtmlHelpers
#model CaptchaTestApp.Models.ColComments
#{
ViewBag.Title = "Home Page";
}
<div class="jumbotron">
<h1>ASP.NET</h1>
<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
<p>Learn more ยป</p>
</div>
<div>
#using (Html.BeginForm())
{
<div> #Html.ValidationSummary(true)</div>
<fieldset>
<legend>ColComments</legend>
<div class="editor-label">
#Html.LabelFor(model => model.fname)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.fname)
#Html.ValidationMessageFor(model => model.fname)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.lname)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.lname)
#Html.ValidationMessageFor(model => model.lname)
</div>
</fieldset>
<table>
<tr>
<td colspan="3">
#Html.Captcha(8, "_captchaCnt")
</td>
</tr>
</table>
<p>
<input type="submit" value="Send" />
</p>
}
</div>
You need to return the same view which you are sending in HttpGet method with updated values. You are sending form collection object to view.
[HttpPost]
public ActionResult Index(FormCollection dataColl)
{
ColComments datgrp = new ColComments();
datgrp.fname = dataColl[0].ToString();
datgrp.lname = dataColl[1].ToString();
if (!this.IsCaptchaValid(""))
{
ViewBag.Classname = "alert alert-warning";
ViewBag.ErrorMessage = "Incorrect captcha answer.";
}
else
{
ViewBag.ErrorMessage = "OKAY";
return RedirectToAction("Index", "Home");
}
return View(datgrp);
}

Submitting image path to the database

Im looking enter in some data and then save it into a DB table,The other fields work just fine when submitted to the database. but when it comes to saving the image path it gives null. Ive searched online and everything i find is out of date and doesn't work for the most part.
Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Insert([Bind(Include ="id, ImageName, ImageSize")] ImageInfo info, HttpPostedFileBase ImagePath)
{
if(ModelState.IsValid)
{
if (ImagePath != null)
{
var filename = Path.GetFileName(ImagePath.FileName);
var path = Path.Combine(Server.MapPath("~/Uploads"), filename);
ImagePath.SaveAs(path);
ImagePath.SaveAs(HttpContext.Server.MapPath("~/Uploads") + ImagePath.FileName);
info.ImagePath = ImagePath.FileName;
}
db.ImageInfoes.Add(info);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(info);
}
View
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Image</legend>
<div class="editor-label">
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.id, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.id, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.id, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ImageName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ImageName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ImageName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ImageSize, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ImageSize, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ImageSize, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#*#Html.TextBoxFor(model => model.ImagePath, new { type = "file" })*#
<input type="file" name="ImagePath" id="ImagePath" />
#Html.ValidationMessageFor(model => model.ImagePath, "", new { #class = "text-danger" })
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</div>
</fieldset>
DB
Any help would be great!.
You want to add enctype="multipart/form-data" to form tag.
I personally like to rename HttpPostedFileBase parameter name to file not to confuse with ImageInfo.ImagePath.
View
#using (Html.BeginForm("Index","Start",FormMethod.Post,new {enctype="multipart/form-data"}))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Image</legend>
<div class="editor-label">
<div class="form-horizontal">
<hr/>
...
<div class="form-group">
#* Notice name and id are named as file *#
<input type="file" name="file" id="file"/>
#Html.ValidationMessageFor(model => model.ImagePath, "", new {#class = "text-danger"})
</div>
...
</div>
</div>
</fieldset>
}
Model
public class ImageInfo
{
public string Id { get; set; }
public string ImageName { get; set; }
public string ImageSize { get; set; }
public string ImagePath { get; set; }
}
Action Method
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index([Bind(Include = "id, ImageName, ImageSize")] ImageInfo info,
HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file != null)
{
file.SaveAs(Server.MapPath("~/Uploads") + file.FileName);
info.ImagePath = file.FileName;
}
/*db.ImageInfoes.Add(info);
db.SaveChanges();*/
return RedirectToAction("Index");
}
return View(info);
}

ASP.NET MVC: Values are null when they reach the Controller

So I have the following Controller:
[HttpPost]
public ActionResult CreateSupport(CreateSupport model)
{
if (ModelState.IsValid && (model.Description != null))
{
model.CreatedById = UserId;
model.ModifiedById = UserId;
}
return View(model);
}
I have the following view:
#using (Html.BeginForm("CreateSupport", "Support", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend></legend>
<div class="editor-label">
#Html.LabelFor(model => model.Subject, new Dictionary<string, object>() { { "class", "req" } })
</div>
<div class="editor-field">
#Html.TextBoxFor(m => m.Subject)
#Html.ValidationMessageFor(model => model.Subject)
</div>
<div class="support-form-left">
<div class="editor-label">
#Html.LabelFor(model => model.BrowserInfo, new Dictionary<string, object>() { { "class", "req" } })
</div>
<div class="editor-field">
#Html.TextBoxFor(m => m.BrowserInfo)
#Html.ValidationMessageFor(model => model.BrowserInfo)
</div>
</div>
<div class="support-form-right">
<div class="editor-label">
#Html.LabelFor(model => model.DatabaseVersion, new Dictionary<string, object>() { { "class", "req" } })
</div>
<div class="editor-field">
#Html.TextBoxFor(m => m.DatabaseVersion)
#Html.ValidationMessageFor(model => model.DatabaseVersion)
</div>
</div>
<div class="clearFloat"></div>
<div class="editor-label">
#Html.LabelFor(model => model.Description, new Dictionary<string, object>() { { "class", "req" } })
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Description)
#Html.ValidationMessageFor(model => model.Description)
</div>
<div class="actionButtons">
<button id="btnCancel" class="myButtonCancel">Cancel</button>
<input type="submit" value="Submit" class="myButton" />
</div>
#if (ViewBag.SuccessMessage != null)
{
<div>
<label style="color: red;">#ViewBag.SuccessMessage</label>
</div>
}
</fieldset>
}
Here's the Model:
public class CreateSupport : SupportTicket
{
public CreateSupport()
{
ProductList = new List<Product>();
ProductVersionsList = new List<ProductVersion>();
EnviromentList = new List<InstallationEnvironment>();
content = new Content();
}
[Required]
[UIHint("tinymce_jquery_full"), AllowHtml]
public string Description { get; set; }
[Required]
[DisplayName("Browser version Info.")]
public string BrowserInfo { get; set; }
[Required]
[DisplayName("Database Version")]
public string DatabaseVersion { get; set; }
public Content content { get; set; }
}
The problem is that the values that reach the Controller are NULL even if you enter some value in them.
You should check your browser's developer tools to see if the form is properly posting its values. If it isn't, you should do two things:
A) Disabled javascript to see if there is a script that is interfering with the POST (typically either by disabling or clearing fields)
B) Ensuring your markup is valid using the W3C markup validation service
For input fields use
#Html.EditorFor(x => x.Subject)
For display fields use
#Html.DisplayFor(x => x.Subject)

MVC4 Pass custom view Model back to Controller Action

I have a custom class
public class BloggerViewModel
{
public Person Blogger;
public List<BloggerWebsite> BloggerWebsites;
}
That I pass into a view
[HttpGet]
public ActionResult Edit(int id)
{
blogger = GetById(id);
var WebSites = GetBloggersWebsites(Id);
var BloggerViewModel = new BloggerViewModel();
BloggerViewModel.Blogger = blogger;
BloggerViewModel.BloggerWebsites = WebSites;
return View(BloggerViewModel);
}
Then when I post back to the edit action
[HttpPost]
public ActionResult Edit(BloggerViewModel entity)
{
return View(entity);
}
entity is null.
My view is something like this ( I took out allot of the code that you didn't need to see. Such as most of the text box bindings)
#model Bloginect.Model.Models.BloggerViewModel
<h2>Edit</h2>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Person</legend>
#Html.HiddenFor(model => model.Blogger.Id)
<div class="editor-label">
#Html.LabelFor(model => model.Blogger.FirstName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Blogger.FirstName)
#Html.ValidationMessageFor(model => model.Blogger.FirstName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Blogger.LastName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Blogger.LastName)
#Html.ValidationMessageFor(model => model.Blogger.LastName)
</div>
<div class="editor-field">
#if (Model.BloggerWebsites[0].Website != null)
{
Html.EditorFor(model => model.BloggerWebsites[0].Website);
}
else
{
#Html.TextBox("Website1")
}
</div>
<div class="editor-field">
#if (Model.BloggerWebsites[1].Website != null)
{
Html.EditorFor(model => model.BloggerWebsites[0].Website);
}
else
{
#Html.TextBox("Website2")
}
</div>
<div class="editor-field">
#if (Model.BloggerWebsites[2].Website != null)
{
Html.EditorFor(model => model.BloggerWebsites[0].Website);
}
else
{
#Html.TextBox("Website3")
}
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
Is there something I am doing wrong? I have checked out some of the other replies to similar questions on this and they did not answer my question.
You must use editors to handle the bindings to your nested model properties, a good example on how to do it can be found here.

Having trouble with ASP.NET MVC 4 image uploading

I'm trying to upload a image along with other fields. Been following examples from here and here
However it seems that the image object isn't passed to the controller and i'm unable to find any error.
Here's the view:
#model Project.Models.ContentNode
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm("Create", "News", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.ValidationSummary(true)
<fieldset>
<legend>News</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Title)
#Html.ValidationMessageFor(model => model.Title)
</div>
#*Image upload field*#
<div class="editor-field">
<input type="file" name="file" />
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Body)
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.Body)
#Html.ValidationMessageFor(model => model.Body)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
Here are the controller methods:
public ActionResult Create()
{
var model = new ContentNode();
return View( model );
}
[HttpPost]
public ActionResult Create(ContentNode nyF, HttpPostedFileBase imageData)
{
// Get the type of content from DB
var k = (from ct in _db.ContentTypes
where ct.ID == 1
select ct).Single();
var curUser = (from u in _db.Users
where u.Username == User.Identity.Name
select u).Single();
nyF.Author = curUser;
nyF.ContentType = k;
nyF.dateCreated = DateTime.Now;
_db.ContentNodes.Add(nyF);
_db.SaveChanges();
// Process image
if ((imageData != null && imageData.ContentLength > 0))
{
var fileName = Path.GetFileName(imageData.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
imageData.SaveAs(path);
}
else
{
return Content("The image object wasn't received");
}
return View(nyF);
}
I've read over the whole thing over and over and am unable to see the error. Anyone here willing to point out what I'm doing wrong?
The name of the input file needs to match the action parameter
<input type="file" name="file" />
should be
<input type="file" name="imageData" />
or you change your action parameter name
public ActionResult Create(ContentNode nyF, HttpPostedFileBase file)
You can upload file, using Request.Files in your post method.
You can validate uploaded file using following code.
Request.Files[0].ContentLength

Resources