Search code examples
c#angularjsasp.net-mvcasp.net-web-apivalidate-request

MVC Controller, A model with same equivalent incoming parameter, is invalid


I'm wonder how to ask this question, but somehow i have to...

TL;DR. I had worked on a system, passed to me by someone else, running on someplace, that they came with their wired request and idea. so i said let do the X thing and make it done, but as i just run it, i saw it didn't worked as it used to... debugging the app, i reach to place, testing testing testing and looking, like my head crashed on that part a whole day, and only thing i could say, why everything is right, why the data exists, why it's valid, and still the controller say it's invalid :|

Well. Today i came back to same project, same point of issue, and here's what i saw:

this is my sent request:

{
    "Code" : 16,
    "StampForm" : {
        "$error" : {},
        "$name" : "stampForm",
        "$dirty" : true,
        "$pristine" : false,
        "$valid" : true,
        "$invalid" : false,
        "$submitted" : true,
        "confirmUser" : {
            "$viewValue" : "a",
            "$modelValue" : "a",
            "$validators" : {},
            "$asyncValidators" : {},
            "$parsers" : [],
            "$formatters" : [null],
            "$viewChangeListeners" : [],
            "$untouched" : false,
            "$touched" : true,
            "$pristine" : false,
            "$dirty" : true,
            "$valid" : true,
            "$invalid" : false,
            "$error" : {},
            "$name" : "confirmUser",
            "$options" : null
        },
        "ReqNo" : "2",
        "ConfirmUser" : "a",
        "SabtDate" : "1395/06/15"
    },
    "MouseData" : {
        "locLeft" : 250.5,
        "locTop" : 395.53125,
        "width" : 812,
        "height" : 663,
        "mouseX" : 223.5,
        "mouseY" : 186.46875
    }
}

And the model i use:

public class StampForm
{
    public string ReqNo { get; set; }
    public string SabtDate { get; set; }
    public string FlightRef { get; set; }
    public string HotelRef { get; set; }
    public string ConfirmUser { get; set; }
    public string PassengerNum { get; set; }
    public string Price { get; set; }
    public string FlightNo1 { get; set; }
    public string FlightNo2 { get; set; }
    public string TicketSrv { get; set; }
    public string VoucherSrv { get; set; }
    /// <summary>
    /// تنظیم کننده
    /// </summary>
    public string Corrector { get; set; }
    /// <summary>
    /// اقامت
    /// </summary>
    public string Stay { get; set; }
    public string PersonPrc { get; set; }
    public string RoomPrc { get; set; }
    public CartableStampPositions Position { get; set; }
    public string Description { get; set; }
}

public class MouseData
{
    public float LocLeft { get; set; }
    public float LocTop { get; set; }
    public float LocRight { get; set; }
    public float LocBottom { get; set; }
    public float Width { get; set; }
    public float Height { get; set; }
    public float MouseX { get; set; }
    public float MouseY { get; set; }
}

public class StampVM
{
    public int Code { get; set; }
    public StampForm StampForm { get; set; }
    public MouseData MouseData { get; set; }
}

And here while i debug:

enter image description here

Do you see that? 'ConfirmUser' is filled with "a" and it generate error, but more than that, it's not 'ConfirmUser', it's 'confirmUser' which start with small 'c', and come from other angular object, which has same name, it even doesn't exists within my model, but since it has same name, the controller let it in, and let it get involved. cause me lot of issue and headache. now that i caught it, i have to solve it, but how should i tell the controller to ignore it?

Update, i forgot to mention that tell you i used MVC.Net WebAPI 2 controller, not the MVC Controller


Solution

  • There is a workaround for this- Add a JObject property confirmUser' in your viewmodel so that the (unwanted) confirm user object binds to that object instead of Model binder trying to bind thatobjecttostring`:

     public class StampForm
        {
            public string ReqNo { get; set; }
            public string SabtDate { get; set; }
            public string FlightRef { get; set; }
            public string HotelRef { get; set; }
            public string ConfirmUser { get; set; }
    
            [JsonProperty("confirmUser")]
            public JObject User { get; set; }
       ..........
    }
    

    And the result will be: enter image description here

    enter image description here