0

I am working on Asp.net WebApi2 and I want to send Model + validation to client.because then with validation client application can put those validations.

I can validate data sent by client application by creating class

public class CalibrationEntity : BaseEntity
{
    [Required(AllowEmptyStrings = false)]        
    [RegularExpression(@"\d{0,3}", ErrorMessage = "The calno field must be in format XXX")]
    public string calno { get; set; }

    [Required(AllowEmptyStrings = false)]
    [DataType(DataType.DateTime)]        
    public Nullable<System.DateTime> dte_createdon { get; set; }

    [StringLength(10)]        
    public string equip_mode { get; set; } 
}

I can use above class to validate data sent by client application to API but How can can I sent Model and validations to client ??

INDIA IT TECH
  • 1,902
  • 4
  • 12
  • 25
Mahajan344
  • 2,492
  • 6
  • 39
  • 90

1 Answers1

0

You can return a HTTP 400 BadRequest together with the ModelState.

    [Authorize(Roles = "Admin, Secretary")]
    [HttpPost]
    [ActionName("Create")]
    public IHttpActionResult PostCreate(SalesOrderRequestModel request)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }            

        return Ok();
    }

This will make the API return a response like:

{"Message":"The request is invalid.","ModelState":{"model.Email":["Email is required."],"model.FirstName"
:["First name must be 2-30 characters in length.","First name is required."],"model.LastName":["Last
 name must be 2-30 characters in length.","Last name is required."],"model.CountryCode":["Country is
 required."],"model.Address1":["Address must be 5-150 characters in length.","Address is required."]
,"model.Role":["Role is required."]}}

If you are using JavaScript here is a code that populates an array with the ModelState errors.

function getModelState(data) {

    var errors = [];
    if (data != undefined) {
        for (var key in data.ModelState) {
            for (var i = 0; i < data.ModelState[key].length; i++) {
                errors.push(data.ModelState[key][i]);
            }
        }
    }

    return errors;

};

You can now pass the entire response from the API to this function and it will return a string array containing all the validation errors.

  • Why would this be post request ?? Its client application which send request to get Model(having data) + list of validations on fields.. then Why would i return Bad request – Mahajan344 Mar 16 '16 at 05:34
  • This is just an example web api. The point is you can return BadRequest because the request failed to pass your validations. So you will return BadRequest together with the specified reasons why the request failed. – John Ephraim Tugado Mar 16 '16 at 05:40
  • But I don't want to send bad request. I want return OK with model having data + validations – Mahajan344 Mar 16 '16 at 05:41
  • The client can now display the cause why the request failed on the user interface. – John Ephraim Tugado Mar 16 '16 at 05:41
  • You are not getting what I am trying to say...First of all client request data.. And API sends data from table along with list of validation on that tables columns... It will be GET – Mahajan344 Mar 16 '16 at 05:44
  • 1
    This is how I understood your question. Next time, improve it so that there won't be misunderstandings. To answer that, you can create a list of string which contains the validation messages and return that to the client. There is no way for you to return the implementation of the model validations being performed by System.ComponentModel.DataAnnotations back to the client. – John Ephraim Tugado Mar 16 '16 at 05:50