Jean J. Michel
Jean J. Michel

Reputation: 591

Parameter's value is null on put method

This is the model:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;

namespace CollectionsCatalogWebAPI.Models
{
    [Table("User")]
    public class User
    {

        #region attributes

        private Int64 id;
        private String username;
        private String firstName;
        private String middleInitials;
        private String lastName;
        private String password;
        private String gender;
        private String active;

        #endregion

        #region properties

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Int64 Id
        {
            set { this.id = value; }
            get { return this.id; }

        }

        [StringLength(50)]
        public String Username
        {
            set { this.username = value; }
            get { return this.username; }
        }

        [StringLength(50)]
        public String FirstName
        {
            set { this.firstName = value; }
            get { return this.firstName; }
        }

        [StringLength(10)]
        public String MiddleInitials
        {
            set { this.middleInitials = value; }
            get { return this.middleInitials; }
        }

        [StringLength(50)]
        public String LastName
        {
            set { this.lastName = value; }
            get { return this.lastName; }
        }

        [StringLength(64)]
        public String Password
        {
            set { this.password = value; }
            get { return this.password; }
        }

        [StringLength(1)]
        public String Gender
        {
            set { this.gender = value; }
            get { return this.gender; }
        }

        [StringLength(1)]
        public String Active
        {
            set { this.active = value; }
            get { return this.active; }
        }

        #endregion

        #region especifcMethods

        /// <summary>
        /// Returns a string representation of this instance.
        /// </summary>
        /// <returns>Returns a string representation of this instance.</returns>
        public override String ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("[CollectionsCatalogWebAPI.Models.User:");
            sb.Append(" Username: ");
            sb.Append(this.Username);
            sb.Append(" FirstName: ");
            sb.Append(this.FirstName);
            sb.Append(" MiddleInitials: ");
            sb.Append(String.IsNullOrEmpty(this.MiddleInitials) ? "" : this.MiddleInitials);
            sb.Append(" LastName: ");
            sb.Append(String.IsNullOrEmpty(this.LastName) ? "" : this.LastName);
            sb.Append(" Gender: ");
            sb.Append(String.IsNullOrEmpty(this.Gender) ? "" : this.Gender);
            sb.Append(" Active: ");
            sb.Append(String.IsNullOrEmpty(this.Active) ? "" : this.Active);
            sb.Append("]");

            return sb.ToString();
        }

        #endregion
    }
}

And now on controller the two methods GET is ok, but the POST is not working.

using System;
using System.Collections.Generic;
using System.Linq;
using CollectionsCatalogWebAPI.Config;
using CollectionsCatalogWebAPI.Models;
using Microsoft.AspNetCore.Mvc;

namespace CollectionsCatalogWebAPI.Controllers
{
    [Produces("application/json")]
    [Route("api/Users")]
    public class UsersController : Controller
    {

        private readonly CollectionsCatalogContex context;

        public UsersController(CollectionsCatalogContex context)
        {
            this.context = context;
        }

        [HttpGet]
        public IEnumerable<User> Get()
        {
            return context.UsersModel.ToList<User>();
        }

        [HttpGet("{id}", Name = "Get")]
        [ProducesResponseType(200, Type = typeof(User))]
        [ProducesResponseType(404)]
        public IActionResult Get(Int64 id)
        {
            var user = context.UsersModel.Find(id);

            if (user == null)
            {
                return NotFound();
            }

            return Ok(user);
        }

        // POST: api/Users
        [HttpPost]
        public IActionResult Post(User u)
        {
            context.UsersModel.Add(u);
            context.SaveChanges();

            return CreatedAtRoute("Get", new { id = u.Id}, u);
        }
    }
}

When I post using Postman the json below I receive an SQL error saying "field active can't be null".

{
    "Id":0,
    "Username":"TEST_POST",
    "FirstName":"POSTADO",
    "MiddleInitials":"API",
    "LastName":"PELO POSTMAN",
    "Password":"senha123",
    "Gender":"M",
    "Active":"N"
}

(without Id information return the same error)

But on a debug I saw that the parameter User (u) is null.

I can see it on ToString:

u   {[CollectionsCatalogWebAPI.Models.User: Username:  FirstName:  MiddleInitials:  LastName:  Gender:  Active: ]}

Some tip about this?

Upvotes: 1

Views: 1744

Answers (2)

Tseng
Tseng

Reputation: 64278

For the sake of competition:

Using of [FromBody] is not required when the controller is annotated with the [ApiController] Attribute (only supported in ASP.NET Core 2.1 and newer). It's a convenience method to reduce the number of required attributes on actions and action parameters.

Among other convenience methods, there is also a typed ActionResult<T> for WebAPI-esque actions and controllers.

You can get more information from the following blog post on ASP.NET Core 2.1.0-preview1: Improvements for building Web APIs

[ApiController] and ActionResult

ASP.NET Core 2.1 introduces new Web API controller specific conventions that make Web API development more convenient. These conventions can be applied to a controller using the new [ApiController] attribute:

  • Automatically respond with a 400 when validation errors occur – no need to check the model state in your action method
  • Infer smarter defaults for action parameters: [FromBody] for complex types, [FromRoute] when possible, otherwise [FromQuery]
  • Require attribute routing – actions are not accessible by convention-based routes

You can also now return ActionResult from your Web API actions, which allows you to return arbitrary action results or a specific return type (thanks to some clever use of implicit cast operators). Most Web API action methods have a specific return type, but also need to be able to return multiple different action results.

Upvotes: 1

Kostis
Kostis

Reputation: 963

You must use the [FromBody] like so:

public IActionResult Post([FromBody]User u)

That way you bind data from the body of the request to User object

Upvotes: 2

Related Questions