Asp.net core model doesn't bind from form

后端 未结 10 883
没有蜡笔的小新
没有蜡笔的小新 2021-02-02 13:54

I catch post request from 3rd-side static page (generated by Adobe Muse) and handle it with MVC action.

相关标签:
10条回答
  • 2021-02-02 14:16

    I'm not sure it is same case, but I had same problem and nothing really looks to work for me.
    The problem in my case was that I had a property called Model in my view model class

    public string Model { get; set; }
    

    When I renamed the property to ModelName everything was working fine again, even without FromForm attribute.

    Looks like some special property names could be a bit of a problem for asp.net mvc model binding.

    So, my advice is to check your model properties and maybe try renaming them one by one in order to check if the problem is there.

    Hope this helps.

    0 讨论(0)
  • 2021-02-02 14:22

    This issue can also happen if one or more of your properties in the request model fail to bind into an acceptable on the request model.

    In my case, I was supposed to pass a List<string> type to a property, but accidentally passed a string. This resulted in the entire request model becoming null

    0 讨论(0)
  • 2021-02-02 14:23

    I have run in to this issue where the model does not bind when I have more than one constructor on the underlying model like:

    public class EmailModel
    {
        public EmailModel()
        {}
    
        public EmailModel(string _name, string _company)
        {
            Name = _name;
            Company = _company;
        }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Company { get; set; }
        public string Phone { get; set; }
        public string Additional { get; set; }
    }
    

    Fixed by removing all but one constructor like this:

    public class EmailModel
    {
        public EmailModel()
        {}
    
        public string Name { get; set; }
        public string Email { get; set; }
        public string Company { get; set; }
        public string Phone { get; set; }
        public string Additional { get; set; }
    }
    
    0 讨论(0)
  • 2021-02-02 14:24

    ASP.Net Core 3.1

    In my case, I was using a complex model (a model that contains other models, like a shared model) posted by Ajax, so the inputs in the view were automatically named like this "ChildModel.PropertyName" (see code)

       [HttpPost]
       [ValidateAntiForgeryToken] // ("AUVM.PropertyName")
       public async Task<JsonResult> AddUser([FromForm]AUVM aUVM) //Parameter name must match the first part of the input name in order to bind
       {
    
       }
    
       [HttpPost]
       [ValidateAntiForgeryToken]
       public async Task<JsonResult> AddUser([FromForm]AUVM someUniqueName) //This is wrong and won't bind
       {
    
       }
    
    0 讨论(0)
  • 2021-02-02 14:24

    In my case, I was using the MVC 4.0 convention of naming the object you are posting. E.g.,

    js: $http.post("SaveAnswer", { answer: answerValues })

    C#: public ActionResult SaveAnswer([FromBody] AnswerVM answer) {...}

    When I changed the js to $http.post("SaveAnswer", answerValues), everything works.

    0 讨论(0)
  • 2021-02-02 14:25

    Change void to ActionResult.

    [HttpPost]
    public ActionResult Index(EmailModel email)
    

    And don't forget verifying AntiForgeryToken from your view and action.

    // to your form in view 
    @Html.AntiForgeryToken()
    // ------------
    
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Index(EmailModel email)
    
    0 讨论(0)
提交回复
热议问题