Form not passing POST data to controller - c#

I am passing in a reference to a file, and a string value cardid. For some reason when I return the result the cardid value is null. However when I view the page the <h3> header contains the string I expected. What am I doing wrong?
HTML view
<h3>#Model.CV.Id</h3>
#using (Html.BeginForm("UploadFile", "Tickets", FormMethod.Post, new { cardid=#Model.CV.Id, enctype = "multipart/form-data"}))
{
<input type="file" name="file" />
<input type="submit" value="Upload Attachment" />
}
Controller
public async Task<IActionResult> UploadFile(string cardid,IFormFile file)
{
......
string res="yay";
return Ok(new {cardid=cardid,res=res });
}

You should include cardid in your form. You are simply creating an attribute on <form>, which does nothing.
<h3>#Model.CV.Id</h3>
#using (Html.BeginForm("UploadFile", "Tickets", FormMethod.Post, new { enctype = "multipart/form-data"}))
{
<input type="hidden" name="cardid" value="#(Model.CV.Id)" />
<input type="file" name="file" />
<input type="submit" value="Upload Attachment" />
}
You could also include `cardid in the action:
Html.BeginForm("UploadFile?cardid=" + Model.CV.Id, "Tickets", FormMethod.Post...)

Related

Cannot insert explicit value for identity column in table

Salaamun Alekum
I Am Getting NULL in file instance of action after posting a file in FORM
public ActionResult CreateDoctorProfile(HttpPostedFileBase file)
{
int LoggedInPersonID = Convert.ToInt32(Session["LoggedInPersonId"]);
erx_t_personnel PersonnelInformation = db.erx_t_personnel.Where(PersonnelInformation1 => PersonnelInformation1.Person_Id == LoggedInPersonID).FirstOrDefault();
return View(PersonnelInformation);
}
This Is My View
#model Doctors_Search_Engine.Models.erx_t_personnel
#{
ViewBag.Title = "Personnel Registration";
}
<h2>Personnel Registration</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<input type="file" name="file" />
<input type="submit" value="Create" class="btn btn-default" />
}
What Should I Do To Get Value In file Of Action Parameters. I Need Guidance On This
Thank You
The problem you have and the subject of your question are totally different. It's confusing.
If you are having an issue passing the file to your controller then your input file name should match the parameter in your controller method.
Change <input type="file" name="Image" /> to <input type="file" name="file" />
Or change your action result to:
public ActionResult CreateDoctorProfile(HttpPostedFileBase Image){}
You also need to set the encryption type to your form.
using (Html.BeginForm(new { enctype = "multipart/form-data" }))

Html.BeginRouteForm accept-charset attribute

I'm building a form in Razor like below:
#using (Html.BeginRouteForm("foo", new { controller = "foo", action = "bar" }, FormMethod.Post, new { id="foo", enctype="multipart/form-data", accept-charset="utf-8" }))
{
<label for="file">File</label>
<input type="file" name="file" id="file" />
<input type="submit" value="Send"/>
}
I need to get some attributes in the form tag. But the compiler doesn't like the dash in accept-charset. How might I allow an object property in C# have a dash?
Use an underscore in the property name: accept_charset
MVC automatically convert underscores in html attribute properties to dashes:
#using (Html.BeginRouteForm("foo", new { controller = "foo", action = "bar" }, FormMethod.Post, new { id="foo", enctype="multipart/form-data", accept_charset="utf-8" }))
{
<label for="file">File</label>
<input type="file" name="file" id="file" />
<input type="submit" value="Send"/>
}
Credit: How to use dashes in HTML-5 data-* attributes in ASP.NET MVC

MVC BeginForm No Data Posted

I am using MVC BeginForm with code below, i cannot get the value fpr input controls in my controller. am i doing anytyhing wrong here?
using (Ajax.BeginForm("CreateApp", "App",
new AjaxOptions { UpdateTargetId = "my-modal-dialog", OnBegin = "Dialog.Closing()", OnSuccess = "Dialog.Close()" },
new
{
#class = "appform"
}
))
{
<input id="newAppName" type="text" size="35" value="" />
#Html.TextBoxFor(model => model.Application.AppName);
<input type="submit" value="Start App" class="demo-button ui-state-default ui-corner-all" />
}
My Controller looks like this
[HttpPost]
public ActionResult CreateApp(AppContent app, string newAppName)
{
}
try changing
<input id="newAppName" type="text" size="35" value="" />
to
<input name="newAppName" type="text" size="35" value="" />

Trouble uploading file in MVC3

I have the following image upload code and controller. The hidden ID is POSTing successfully, but the image remains null.
The form:
#using(Html.BeginForm()){
<input type="hidden" name="merchandiseId" id="id" value="#ViewBag.Id"/>
<input type="file" name="image" id="image" />
<input type="submit" />
}
The controller:
[HttpPost]
public ActionResult AddImage(int merchandiseId, HttpPostedFileBase image)
Debugging and stepping in verifies that image is null while merchandiseId has the correct value.
You need to add the enctype = "multipart/form-data" in your form. otherwise no file will be uploaded.
An example
#using (Html.BeginForm("UploadAction", "MyController", new { Model.Id }, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="myFile">
<button type="submit>Upload</button>
}
Try adding new { enctype = "multipart/form-data" }to your form

Get files for each language in model (MVC 3) using HttpContext to save them in separate table

I have this simple piece of HTML code:
<div>
<input type="file" name="english-file" />
</div>
<div>
<input type="file" name="french-file" />
</div>
I used in my model, the following C# line code:
object receivedFiles = HttpContext.Current.Request.Params["english-file"];
but that object returns null always.
Of course that I can use HttpContext.Current.Request.Files but I want to sepparate files by languages (the above HTML code is simple but someone could add more files to english than french with +Add files button) and save them in sepparate table.
How could I do that?
That's not how files are uploaded. You should look in Request.Files["english-file"] after setting the proper enctype on the form.
Let's take an example:
#using (Html.BeginForm("upload", "somecontroller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
<input type="file" name="english" />
</div>
<div>
<input type="file" name="french" />
</div>
<button type="submit">Upload</button>
}
and in the controller action:
[HttpPost]
public ActionResult Upload()
{
var englishFile = Request.Files["english"];
var frenchFile = Request.Files["french"];
...
}
or even better:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase english, HttpPostedFileBase french)
{
...
}
or even better:
[HttpPost]
public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
{
...
}
assuming you adjust the names of the file inputs:
#using (Html.BeginForm("upload", "somecontroller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
<input type="file" name="files" />
</div>
<div>
<input type="file" name="files" />
</div>
<button type="submit">Upload</button>
}
I also invite you to read the following blog post.

Categories