I have a form that uploads image files and checks that they're jpgs:
// CarAdmin/Index.cshtml
@model MySite.Models.Car
@using (Html.BeginForm("CarImageUpload", "Car", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type="text" name="imageInfo" />
<input type="submit" value="OK" />
}
<form action="CarAJAX" method="post" name="CarAdminForm">
<input name="Make" value="@Model.Name/>
<input type="submit" value="Update Car Info">
</form>
// CarController.cs
[HttpPost]
public ActionResult CarImageUpload(HttpPostedFileBase file)
{
ValidateImageFile V = new ValidateImageFile(file); // checks that the file is a jpg
List<String> Validity = V.Issues;
if (Validity.Count == 0)
{
file.SaveAs(V.FilePath);
}
else
{
Response.Write(String.Join("<br>", Validity.ToArray()); // THIS IS PROBLY WRONG
}
RedirectToAction("CarAdmin");
}
public ActionResult CarAdmin()
{
return View("CarAdmin/Index.cshtml");
}
If the ValidateImageFile class finds a problem, I want to:
- give input that had an issue a class
- display a message on the page
However, I'm not sure how to manipulate forms from the Controller, and my Response.Write is not sending back anything (that I can see - but I'm not sure how to access that).
I have a few ideas on how to accomplish this, but they seem like a duct tape job, rather than best practice.