How can ModelState be accessed in HttpPut if it is only created in HttpPost?

“The ModelState represents a collection of name and value pairs that were submitted to the server during a POST.” That is the best definition I have found for the ModelState property. So I have some code using a web api that Creates a customer and Updates a customer, and my question is, since I have accessed ModelState in CreateCustomer, wont the ModelState always be not valid if I hadnt already submitted a Post request with CreateCustomer? Or is the ModelState updated with both PUT and Post requests? Because according to the definition above, the ModelState property wont be initialized until I create a customer, and in my UpdateCustomer method, I need to check if the customer object given in the parameters is the one that is valid, not any Model that was submitted to the server during a different POST request.

[HttpPost]
    //If we name it PostCustomer, we dont have to put the httppost tag
    public Customer CreateCustomer(Customer customer)
    {
        if (!ModelState.IsValid)
        {
            throw new HttpResponseException(HttpStatusCode.BadRequest);
        }
        _context.Customers.Add(customer);
        _context.SaveChanges();
        return customer;
    }

    // Put /api/customers/1
    [HttpPut]
    public void UpdateCustomer(int id, Customer customer)
    {

        if (!ModelState.IsValid)
        {
            throw new HttpResponseException(HttpStatusCode.BadRequest);
        }
        
        Customer customerInDb = _context.Customers.SingleOrDefault(c => c.Id == id);
        
        if (customerInDb == null)
            throw new HttpResponseException(HttpStatusCode.NotFound);

        customerInDb.Name = customer.Name;
        customerInDb.Birthdate = customer.Birthdate;
        customerInDb.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter;
        customerInDb.MembershipTypeId = customer.MembershipTypeId;

        _context.SaveChanges();
    }

Code is from Mosh Hamedani’s ASP.Net MVC course.
Link to the ModelState definition. https://exceptionnotfound.net/asp-net-mvc-demystified-modelstate/

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

That’s an incomplete and somewhat confusing definition. A model is simply how you send data, nothing more. It could be via a GET, POST, PUT whatever. It just models whatever data each endpoint requires, nothing more.

I guess POST was mentioned because that’s the most common scenario, but don’t let it limit your understanding of it.

You might have the same model for a PUT and a POST or they might be a bit different.
For example your entity model may or may not include an ID. POST, which creates data does not need ID, while PUT does.

If the model you use for updating already includes the ID then you might decide to not have the id separate since it’s redundant data. So you see, there are a number of decisions you need to make with an API.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x