0

If I'm posting some variables to a Web API method, is it possible to do this without creating a view model?

It seems that you can only bind to a single parameter (which represents the request's body), so if there are multiple variables then that necessitates a view model to contain them.

1 Answer 1

1

You can only have one param decorated with [FromBody], so therefore that one param must encapsulate all the data coming from the request body.

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

5 Comments

That seems inefficient.
Quite the opposite, in fact. Using multiple params, you just have a loose collection of unrelated data with no validation. With a view model, you have a defined contract, a cohesive set of data, and you can apply validation to ensure that that data is well-formed.
A method's parameter list isn't a loose collection of unrelated data. Good point about validation though.
Unrelated in the sense of independent. It may have meaning to you, but it ends there. The members of a class define the very identity of that class, and thus work together to form a full picture. Not so with params.
The parameters of a method (along with the return type) define the identity of a method. It's a method name instead of a class name and, around the data, it's parentheses instead of curly brackets.

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.