Display form errors using MVC

I have a form that uploads image files and verifies that they are jpg:

// 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 detects a problem, I want:

  • enter the input in which there was a class problem
  • display message on page

However, I am not sure how to manipulate the forms from Controller, and my Response.Write does not send back anything (which I see, but I'm not sure how to access it).

I have a few ideas on how to do this, but they seem to work on duct tape rather than best practice.

+4
source share
1 answer

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); // checks that the file is a jpg
    List<String> issues = validity.Issues;

    if (issues.Count > 0)
    {
        // TODO: Add more descriptive issue messages
        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");
+2

All Articles