0

I'm using model validation in MVC to validate my forms.

I do it like this:

[MetadataType(typeof(CarModel))]
public partial class Car {...}


public class CarModel
{
    [Required(ErrorMessage = "Select the brand please")]
    [Display(Name = "Brand* :")]
    public object brand_id { get; set; }

    ...
 }

In my View I have this code for the dropdownbox:

@Html.DropDownListFor(m => m.brand_id, new SelectList(ViewBag.brands, "id", "name"), new {@class = "selectBoxes" })

And the Viewbag.brands is filled like this:

List<Brand> brands = Brand.GetAllBrands();
ViewBag.brands = brands;

(Brands has the fields "id" (int) "name" (string)), the Car class/table uses "brand_id" as reference.

When I want to sent an empty form, normally Formvalidation should work, but I get an error on the @Html.DropDownListFor:

ArgumentNullException was unhandled by user code
Value cannot be null.

What is the problem? The creation of the dropdownbox? Or isn't formvalidation working correct? The brand-names are loaded correct in the dropdownbox and there is NO "0" value like (-- Select --), the first brand is displayed in the dropdownbox... But when I send an empty form it gets stuck on the dropdownlist...

EDIT: Maybe the problem is that Car uses "brand_id" and Brand uses "id"? If yes, how to solve this without changing this field-names?

EDIT2: Brand is just a partial class of Brands (Linq), with function:

public static List<Brand> GetAllBrands()
{
    db = new CarsDataClassesDataContext();
    return db.Brands.OrderBy(b => b.name).ToList<Brand>();
 }
6
  • Please post the definition of the Brand class. Commented Jan 16, 2014 at 8:36
  • When the dropdown was being created, ViewBag.brands is null. Are you sure your initialization code (ViewBag.brands = brands) was executed? Use a breakpoint to be sure. Can you post your controller? Commented Jan 16, 2014 at 8:45
  • ViewBag.brands is filled because the dropdownbox is shown correct with the brandnames in it, but when I send the form, I get the error... Or am I misunderstanding you? Commented Jan 16, 2014 at 8:48
  • When you said, "send the form" did you mean submit the form? In which case, you must have a GET method that originally obtained the form and a POST method that is for submitting the form. Can you show your action methods? Commented Jan 16, 2014 at 8:53
  • I submit the form to a HttpPost : s24.postimg.org/c5kuig6px/… Commented Jan 16, 2014 at 9:01

1 Answer 1

1

Based from your POST method, if the ModelState is invalid it will return to the same view, in which case you will have to reinitialized ViewBag.brands all over again.

if (ModelState.IsValid)
{
     // perform your logic here
}

// if you reached this code, that means the model validation failed
// so you will have to reinitialize the ViewBag

ViewBag.brands = Brand.GetAllBrands();
return View(car);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.