User Darian Dimitrov answered a question very similar to yours, his solution should point you in the right direction.
HttpPostedFilebase MVC 2?
, :
http://cpratt.co/file-uploads-in-asp-net-mvc-with-view-models/
:
// CarAdmin/Index.cshtml
@model MySite.Models.CarUploadViewModel
@using (Html.BeginForm("CarImageUpload", "Car", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="ImageUpload" />
<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>
:
public class CarUploadViewModel
{
[Required]
public string ImageInfo{ get; set; }
[DataType(DataType.Upload)]
HttpPostedFileBase ImageUpload { get; set; }
}
:
[HttpPost]
public ActionResult CarImageUpload(CarUploadViewModel model)
{
ValidateImageFile validity = new ValidateImageFile(model.ImageUpload);
List<String> issues = validity.Issues;
if (issues.Count > 0)
{
ModelState.AddModelError("ImageUpload", "There was an issue.");
}
if(ModelState.IsValid)
{
model.ImageUpload.SaveAs(V.FilePath);
RedirectToAction("CarAdmin");
}
return View(model);
}
, , , , , , .
, :
ModelState.AddModelError("MyField", "Custom error message here");
, :
@Html.ValidationMessage("MyField");