WarSam
WarSam

Reputation: 27

Controller receives unwanted/additional model data from ASP.NET MVC View

In my ASP.NET MVC solution, I have a model that contains 25 to 30 properties. These properties are used in various forms. However, one of my Edit form (that contains only 15 fields) passes the value to an additional model property (which is required) as shown in the screenshot below. And now since no value was provided to that additional property from the edit form, the ModelState is failing with the error as a null exception.

enter image description here

I searched and re-searched all the code (design and runtime) to check if that additional property is available on the form and erroneously tied to the model while post back, but it isn't.

Model (partial list - properties that are listed in the screenshot):

public int VISITOR_ID { get; set; }

[Required(ErrorMessage = "First Name is required")]
[DisplayName("First Name")]
public string FIRST_NAME { get; set; }

[Required(ErrorMessage = "Last Name is required")]
[DisplayName("Last Name")]
public string LAST_NAME { get; set; }

[Required(ErrorMessage = "Gender is required")]
[DisplayName("Gender")]
public string GENDER { get; set; }

[Required(ErrorMessage = "Birth Year is required")]
[DisplayName("Birth Year")]
public int BIRTH_YEAR { get; set; }

[Required(ErrorMessage = "Address is required")]
[DisplayName("Address")]
public string VISITOR_ADDRESS { get; set; }

[Required(ErrorMessage = "Visitor's State is required")]
[DisplayName("State / International Country")]
public string VISITOR_STATE { get; set; }

[Required(ErrorMessage = "Visitor's City is required")]
[DisplayName("City")]
public string CITY { get; set; }

[Required(ErrorMessage = "Visitor's mobile number is required")]
[DisplayName("Mobile")]
public string MOBILE { get; set; }

[Required(ErrorMessage = "Select an ID type")]
[DisplayName("Photo ID Proof")]
public string ID_TYPE { get; set; }

[Required(ErrorMessage = "Provide an ID number")]
[DisplayName("ID Number")]
public string ID_NUMBER { get; set; }

[Required(ErrorMessage = "Please select Yes/No for the visitor")]
[DisplayName("Is this visitor \"Head of a Family?\"")]
public int IsHOF { get; set; }

public int FAMILYID { get; set; }

[Required(ErrorMessage = "Please select 'Family Members' for the visitor's Family")]
[DisplayName("Select \"Family Members\"")]
public string? FAMILYMEMBERS { get; set; }

[Required(ErrorMessage = "Provide select Yes/No for Niyaz")]
[DisplayName("Niyaz")]
public string NIYAZ { get; set; }

My controller method for Edit form HttpPost:

[HttpPost]
public ActionResult VisitorEdited(AUCModel VisEdtAUCModel)
{ 
    // breakpoint is set here
    try
    {
        if (ModelState.IsValid) // ModelState is invalid
        {
            AUCDataObj AUCDAO = new AUCDataObj();
            int RecCoe = AUCDAO.EditVisitor(VisEdtAUCModel);

            if (RecCoe > 0)
            {
                System.Diagnostics.Debug.WriteLine("Record UPDATED");
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("Naaahh");
            }
        }
            
        return RedirectToAction("Index");
    }
    catch
    {
        System.Diagnostics.Debug.WriteLine("Error - Under Catch");
        return View("Details", VisEdtAUCModel);
    }
}

Upvotes: 1

Views: 49

Answers (1)

Richard   Housham
Richard Housham

Reputation: 1680

Ok I think we need to take a step back and think about what is happening. The model is either valid or invalid. The view and what come from the user can be edited and doctored. So models are basically just trying to always be valid. Behind the scenes it's matching posted values to the modal. On another level when you made your model and migration that field would have been made required in the database.

So basically you have a few options...

  1. Just remove the required field and it will work
  2. You could use ivalidateobject How do I use IValidatableObject? To create a custom rule.
  3. Change your view to post some data, perhaps in a hidden field.
  4. Different modal

End of the day, not posting anything still means the modal is invalid because the required field is blank. That's protecting itself and ensuring that it can't be saved in an invalid state.

Hope that help

Upvotes: 1

Related Questions