I have following data in html table and trying to submit the data but I miss additional data from view to controller and I can only getting uploaded files details. how to pass file details and other data in each row simultaneously. Here I need to pass StudentName1 & StudentName2 along with corresponding file to controller.
<form action="" method="post" enctype="multipart/form-data">
<div class='row'>
<label for="txt">StudentName1:</label>
<input type="text" name="name1" id="name1" />
<label for="file1">Filename:</label>
<input type="file" name="files" id="file1" />
</div>
<div class='row'>
<label for="txt">StudentName2:</label>
<input type="text" name="name2" id="name2" />
<label for="file2">Filename:</label>
<input type="file" name="files" id="file2" />
</div>
<input type="submit" />
</form>
COntroller
[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> files) {
foreach (var file in files) {
if (file.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/Myuploads"), fileName);
file.SaveAs(path);
}
}
return RedirectToAction("Index");
}
Related
I have a basic form for which I want to handle buttons inside the form by calling the ActionResult method in the View's associated Controller class. Here is the following HTML5 code for the form:
<h2>Welcome</h2>
<div>
<h3>Login</h3>
<form method="post" action= <!-- what goes here --> >
Username: <input type="text" name="username" /> <br />
Password: <input type="text" name="password" /> <br />
<input type="submit" value="Login">
<input type="submit" value="Create Account"/>
</form>
</div>
<!-- more code ... -->
The corresponding Controller code is the following:
[HttpPost]
public ActionResult MyAction(string input, FormCollection collection)
{
switch (input)
{
case "Login":
// do some stuff...
break;
case "Create Account"
// do some other stuff...
break;
}
return View();
}
you make the use of the HTML Helper and have
#using(Html.BeginForm())
{
Username: <input type="text" name="username" /> <br />
Password: <input type="text" name="password" /> <br />
<input type="submit" value="Login">
<input type="submit" value="Create Account"/>
}
or use the Url helper
<form method="post" action="#Url.Action("MyAction", "MyController")" >
Html.BeginForm has several (13) overrides where you can specify more information, for example, a normal use when uploading files is using:
#using(Html.BeginForm("myaction", "mycontroller", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
< ... >
}
If you don't specify any arguments, the Html.BeginForm() will create a POST form that points to your current controller and current action. As an example, let's say you have a controller called Posts and an action called Delete
public ActionResult Delete(int id)
{
var model = db.GetPostById(id);
return View(model);
}
[HttpPost]
public ActionResult Delete(int id)
{
var model = db.GetPostById(id);
if(model != null)
db.DeletePost(id);
return RedirectToView("Index");
}
and your html page would be something like:
<h2>Are you sure you want to delete?</h2>
<p>The Post named <strong>#Model.Title</strong> will be deleted.</p>
#using(Html.BeginForm())
{
<input type="submit" class="btn btn-danger" value="Delete Post"/>
<text>or</text>
#Url.ActionLink("go to list", "Index")
}
Here I'm basically wrapping a button in a link. The advantage is that you can post to different action methods in the same form.
<a href="Controller/ActionMethod">
<input type="button" value="Click Me" />
</a>
Adding parameters:
<a href="Controller/ActionMethod?userName=ted">
<input type="button" value="Click Me" />
</a>
Adding parameters from a non-enumerated Model:
<a href="Controller/ActionMethod?userName=#Model.UserName">
<input type="button" value="Click Me" />
</a>
You can do the same for an enumerated Model too. You would just have to reference a single entity first. Happy Coding!
I am trying to allow the user to upload a pdf file. I am not getting any errors, but the IFormFile 'PostedFile' is always null in the controller.
Create View:
<div class="form-group">
<label asp-for="PostedFile" class="control-label"></label>
<div class="col-md-10">
<input type="file" asp-for="PostedFile" />
<span asp-validation-for="PostedFile" class="text-danger"></span>
</div>
Controller, Create method:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,Name,Phone1,Phone2,District_Division,OrgNumber,DateOfTest,DateOfExposure,NumberOfExposed,Notes,PathToFile")] Case_Log case_Log, IFormFile PostedFile)
{
string path = "Case_Log_Docs/";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
System.Diagnostics.Debug.WriteLine("Created the folder.");
}
if (PostedFile != null)
{
string fileName = Path.GetFileName(PostedFile.FileName);
System.Diagnostics.Debug.WriteLine(fileName);
PostedFile.CopyTo(new FileStream(path, FileMode.Create));
ViewBag.Message += string.Format("<b>{0}</b> uploaded.<br />", fileName);
}
else
{
System.Diagnostics.Debug.WriteLine("Posted file was null.");
}
if (ModelState.IsValid)
{
_context.Add(case_Log);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(case_Log);
}
PLEASE NOTE: I (think I) do NOT want to use List as I do NOT want the user to be able to upload more than 1 single document at a time as the documents have a corresponding database entries with a 1 to 1 relationship.
I have a few questions.
1.) What is the problem? Why is IFormFile always null?
2.) Why does it seem like people always recommend List over IFormFile?
Passing the rest of the variables to the controller works fine:
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"> </div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Phone1" class="control-label"></label>
<input asp-for="Phone1" class="form-control" />
<span asp-validation-for="Phone1" class="text-danger"></span>
</div>
But the file upload div is still inside of the form that is directed to the Create method. Is there something wrong with the view element? If so how would I change it to correct the issue?
I tried following this example and got no errors but no results either: https://www.aspsnippets.com/Articles/ASPNet-Core-IFormFile-always-returns-NULL.aspx
You need use enctype=multipart/form-data to allows entire files to be included in the data.Like following.
<form asp-action="xxx" enctype="multipart/form-data">
//...
<input type="file" name="PostedFile" />
<input type="submit" value="click"/>
</form>
Action:
[HttpPost]
public IActionResult Demo(IFormFile PostedFile)
{
//...
}
Result:
I have this html form:
<form method="post" action="/Record">
<input type="text" name="items[0][name]" value="Watch" />
<input type="text" name="items[0][model]" value="Ballon" />
<input type="text" name="items[1][name]" value="Shape" />
<input type="text" name="items[1][model]" value="Bleu" />
<input type="text" name="items[2][name]" value="Accessory" />
<input type="text" name="items[2][model]" value="Hublot" />
<input type="submit" value="SEND" />
</form>
I need to get the value of every item. i try something like this in c# .net
String[] myItems;
myItems = Request.Form.GetValues("items");
foreach (var singleItem in myItems)
{
WriteLine(singleItem.name);
WriteLine(singleItem.model);
WriteLine(singleItem["name"]);
WriteLine(singleItem["model"]);
}
i appreciate your advice
let try as below:
var myItems = Request.Form["items"];
I have a very basic multiple file uploader that works on Firefox and IE but on Chrome it hangs when I include the name="files" attribute in the input tag below.
Has anyone else experienced this problem? and if so is there a work around?
<form action="/AdminFileUpload/PostMultiple" method="post" enctype="multipart/form-data">
<input id="fileupload" type="file" name="files" multiple="multiple" />
<input type="submit" name="submit" value="Upload" />
<hr />
<b>Live Preview</b>
<br />
<br />
<div id="dvPreview">
</div>
</form>
This is the controller I am using;
If I remove name="files" from the input tag the IEnumerable<HttpPostedFileBase> files parameter in the controller is null.
[HttpPost]
public ActionResult PostMultiple(IEnumerable<HttpPostedFileBase> files)
{
foreach (var file in files)
{
if (file != null && file.ContentLength > 0)
{
file.SaveAs(Path.Combine(Server.MapPath("/Uploads"), Path.GetFileName(file.FileName)));
}
}
return RedirectToAction("Index");
}
I am trying to upload a single .csv file in ASP.NET MVC. In my .ascx file, I have:
<div>
<input type="file" name="file" id="file" />
   
<input type="submit" name="btnSubmit" id="btnSubmit" value="Upload" />
</div>
The controller action is:
public ActionResult Upload(HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
return View();
}
The problem is that I always get file as Null in the Upload Action. Any suggestions on how to get this working?
Are you sure you have a
<form enctype="multipart/form-data" method="post">
<div> bla bla
</div>
</form>
?
Edit :
method="post"
+
[HttpPost]
on your action