2

I have This Code in my Truck Controller, every time i run it.

My model is always null.

    [HttpPost]
    public ActionResult AddTruck(TruckDataModel model)
    {
        var newTruck = new truck()
        {
            registration_no = model.reg_no,
            make = model.make,
            model = model.model,
            engine_no = model.engine_no,
            chassis_no = model.chassis_no,
            driver_name = model.driver_name,
            driver_no = model.driver_no,
            current_run = model.current_run,

        };

        if (ModelState.IsValid)
        {
            db.trucks.Add(newTruck);
            db.SaveChanges();
            return RedirectToAction("TruckList","Trucks");
        }

        return View(model);

This is my model

public class TruckDataModel
{         
    [Required(ErrorMessage = "Registration No. required", AllowEmptyStrings = false)]
    [Display(Name = "Registration No.")]
    public string reg_no { get; set; }

    [Required(ErrorMessage = "Make required", AllowEmptyStrings = false)]
    [Display(Name = "Make")]
    public string make { get; set; }

    [Required(ErrorMessage = "Model required", AllowEmptyStrings = false)]
    [Display(Name = "Model")]
    public string model { get; set; }

    [Required(ErrorMessage = "Engine No. required", AllowEmptyStrings = false)]
    [Display(Name = "Engine No.")]
    public string engine_no { get; set; }

    [Required(ErrorMessage = "Chassis No. required", AllowEmptyStrings = false)]
    [Display(Name = "Chassis No.")]
    public string chassis_no { get; set; }

    [Required(ErrorMessage = "Driver Name required", AllowEmptyStrings = false)]
    [Display(Name = "Driver Name")]
    public string driver_name { get; set; }

    [Required(ErrorMessage = "Current Run required", AllowEmptyStrings = false)]
    [Display(Name = "Current Run")]
    public int current_run { get; set; }

    [Required(ErrorMessage = "Driver no. required", AllowEmptyStrings = false)]
    [Display(Name = "Driver No.")]
    public int driver_no { get; set; }


}//just for allow to edit

I have the exception NullReference always occur, and when I breakpoint, the all the model is null.

I have the exact code in my CreateUser Action but it works perfectly fine? Please need help TY SO MUCH!!

2
  • provide your code of View from where this Post action occur Commented Aug 27, 2015 at 5:37
  • @Sac, the view is not necessary :) Commented Aug 27, 2015 at 5:38

1 Answer 1

1

Your TruckDataModel model has a property named model and your POST method parameter is also named model. You need to change one or the other, say

public ActionResult AddTruck(TruckDataModel truck)
{
  ....
}

The reason is that the DefautModelBinder first initializes an instance of TruckDataModel. It then reads the form data, sees a name/value pair for model, then tries to set your parameter to its value (to say model="Ford") which fails and so the model becomes null

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, this helped me very much

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.