Multiple Action of Controller in single Button in MVC 4 - asp.net-mvc

my controller Have 2 Actions :
[HttpPost]
public ActionResult Upload(HttpPostedFileBase[] files)
{
foreach (HttpPostedFileBase file in files)
{
string path = System.IO.Path.Combine(Server.MapPath("~/App_Data"), System.IO.Path.GetFileName(file.FileName));
file.SaveAs(path);
}
ViewBag.Message = "File(s) uploaded successfully";
return RedirectToAction("Index");
}
//
// GET: /AdultLiteracyTeachers/Details/5
public ActionResult Details(int id = 0)
{
AdulLiteracyTeachers adulliteracyteachers = db.AdulLiteracyTeachers.Find(id);
if (adulliteracyteachers == null)
{
return HttpNotFound();
}
return View(adulliteracyteachers);
}
my view in Create.cshtml
#using (Html.BeginForm(Upload, ControllerName, FormMethod.Post, new { enctype = "multipart/form-data" }))
<input type="file" name="files" value="Upload Image" />
<input name="Upload" type="submit" value="Create" />
Problem is
only upload is working when I submit button others create etc not working
how to call multiple actions in single form create button ?

I believe you can use the beginform multiple times like this:
#using (Html.BeginForm(Upload, ControllerName, FormMethod.Post, new { enctype = "multipart/form-data" })){
<input type="submit" name="files" value="Upload Image" />
}
#using (Html.BeginForm(Details, ControllerName, FormMethod.Post, new { enctype = "multipart/form-data" })){
<input name="Upload" type="submit" value="Create" />
}
and make sure that the button type="submit".

Related

unable to get all uploaded file in mvc 5

I have two file upload control with different name and id.
But when i am uploading multiple file ,i am receiving only one file of each control.
Below is my code.
Main View
#using (Ajax.BeginForm("Questionnaire", "Tax", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "divQuestionnaire" }, new { id = "formquestionnaire", enctype = "multipart/form-data" }))
{
<div class="clearfix" id="divQuestionnaire">
#{ Html.RenderPartial("_Questionnaire");}
</div>
}
Partial view where file upload control is placed
<input id="PhotoUrl" type="file" class="upload" multiple="multiple" name="PhotoUrl" />
<input id="AddressUrl" type="file" class="upload" multiple="multiple" name="AddressUrl" />
<button type="submit">Save</button>
controller
public ActionResult Questionnaire(HttpPostedFileBase[] PhotoUrl, HttpPostedFileBase[] AddressUrl)
{
}
also tryied this not working
public ActionResult Questionnaire(IEnumerable<HttpPostedFileBase> PhotoUrl, IEnumerable<HttpPostedFileBase> AddressUrl)
{
}

How to fire action in the controller when I select a file from an input file control found in edit form

I have a code in editform like this.
#using (Html.BeginForm("Bind", "Student", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<img id="sp" src="/Student/RetrieveImage/model.StudentID" alt="Photo" height=100 width=200 />
<input type="file" name="ImageData" id="ImageData" onchange="DI();"/>
}
And this in Student Controller
HttpPostedFileBase file;
public ActionResult Bind()
{
file = Request.Files["ImageData"];
file = Request.Files["ImageData"];
return RedirectToAction("StudentEdit");
}
The begin form is found in editform which is a partial form. What I need is to fire the Bind Action when file is selected from input file. How can I do this?
You are working with an form(html element) to upload the image(with HTTP POST verb), in your example you have two main options to proceed with the upload:
1'st option: Create a submit input inside the form
#using (Html.BeginForm("Bind", "Student", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="ImageData" id="ImageData" onchange="DI();"/>
<input type="submit" name="submitbutton" value="Upload" />
}
2'nd option: submit the form with javascript, in this example I used the event that you created at the file element:
#using (Html.BeginForm("Bind", "Student", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="ImageData" id="ImageData" onchange="this.form.submit();"/>
}

File uploading in MVC 4

I simply tried this, but its not working, what is the problem in it,
MY index page:
#{
ViewBag.Title = "Index";
}
#using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/from- data" }))
{
<div>
<h1 style="align-content: center; color: blueviolet">Application to upload files</h1>
</div>
<div>
<input type="file" id="file" name="file" />
<br />
<input type="submit" id="load" name="submit" value="Submit" />
</div>
}
And My controller is,
[HttpPost]
public ActionResult Upload()
{
string path = #"~/Content/Upload";
HttpPostedFileBase file = Request.Files["file"];
if (file != null)
file.SaveAs(path + file.FileName);
return Content("Sucess");
}
The path you are attempting to save your file to looks wrong. Try with MapPath:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
string path = Server.MapPath("~/Content/Upload");
if (file != null)
{
file.SaveAs(Path.Combine(path, file.FileName));
}
return Content("Sucess");
}
Also make sure that you have used the correct enctype attribute in your form:
enctype = "multipart/form-data"
instead of:
enctype = "multipart/from- data"

MVC 4 HttpPostedFileBase always null

No matter what I try my post images are always null or Object reference not set to an instance of an object.
I have a post class and images class that map to tables (Using EF code first)
Here's my code:
View:
<h2>Create</h2>
#using (Html.BeginForm("Create", "Admin", FormMethod.Post, new{ enctype = "multipart/form-data" }))
{
#Html.ValidationSummary(true)
#Html.LabelFor(post => post.Post.Blog)
#Html.DropDownListFor(post => post.Post.BlogId, Model.BlogList)
#Html.LabelFor(post => post.Post.Category)
#Html.DropDownListFor(post => post.Post.CategoryId, Model.CategoryList)
#Html.LabelFor(post => post.Post.Title)
#Html.EditorFor(post => post.Post.Title)
<br />
#Html.LabelFor(post => post.Post.Content)
#Html.EditorFor(post => post.Post.Content)
<br />
<input type="file" name="uploadImage" value="Upload" />
<input type="submit" value="Create" />
}
Action Method:
[HttpPost]
public ActionResult Create(Post post, HttpPostedFileBase image)
{
if (ModelState.IsValid)
{
var postBlog = _repository.Blogs.Single(b => b.BlogId == post.BlogId);
post.Created = DateTime.Now;
postBlog.Posts.Add(post);
PostImage newImage = new PostImage()
{
MimeType = image.ContentType,
ImageData = new byte[image.ContentLength],
PostId = post.PostId
};
_repository.AddImage(newImage);
_repository.Save();
return RedirectToAction("Index");
}
Doh! the problem was I had set 'value' in the input element you only need input type="file" name="Image" /> for it to bind correctly

MVC 3 input file always null

I added an input file field but it's always null on the controller. What am I missing?
Here's the code for both my view and controller.
view:
...
#using (Html.BeginForm())
{
...
<input type=file name="file" id="file" class="post-attachment" />
...
}
controller:
[HttpPost]
public ViewResult _Details(HttpPostedFileBase file, ViewTopic viewTopic, string SearchField, string submitBtn)
{
// save file to server
if (file != null && file.ContentLength > 0)
{
var fileName = DateTime.Today.ToString("yy.MM.dd") + Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Attachments"), fileName);
file.SaveAs(path);
}
...
}
You need to explicitly set the enctype of the form:
#using(Html.BeginForm("action", "controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
...
}
You need to change your form to something like -
#using (Html.BeginForm("Upload", "File", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<p>
<input type="file" id="fileUpload" name="fileUpload"/>
</p>
<p>
<input type="submit" value="Upload file" /></p>
}
There's a load more info (including the sample above) in this question - Html helper for <input type="file" />

Resources