开发者

ModelBinder not bind the values

开发者 https://www.devze.com 2022-12-10 09:17 出处:网络
Hi I have an action in my controller, i am waiting to ModelBinder bind the incoming postdata to my action parameter but it doesnt do this, i dont know. I only manually bind them with TryUpdateModel()

Hi I have an action in my controller, i am waiting to ModelBinder bind the incoming postdata to my action parameter but it doesnt do this, i dont know. I only manually bind them with TryUpdateModel() but i dont want to use it. Property names and the postdata are the same why it cant bind them ?

public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public stri开发者_开发问答ng Username { get; set; }
    public string Password { get; set; }
}

public ActionResult Update(User user)
{
    // TODO
}

My javaScript Code that send data to my Action:

newData = {
   "FirstName":"Yucel"
   "LastName": "Akpınar",
   "Email": "yucelakpinar",
   "Password": "123456"
}; 

    $.ajax({
       type: "POST",
       dataType: "json",
       data: newData,
       url: "/Profile/Update",
       success: function() {
           debugger;
       }
    });


The reason it isn't working is because the payload of your request is a JSON object, which the current model binder isn't capable of parsing (actually the model binder doesn't retrieve any of its values, the value provider does). We've made this scenario possible in v2, but for it to work in v1 you'd have to add support for it yourself.


I fix the problem. The reason is ModelState is not valid.

Because of the Validation Attribute ModelBinder doesnt bind the properties if there is a NotValid situation...

[MetadataType(typeof(UserMetaData))]
public partial class User
{
    public string FirstName { get; set; }
    ....
    ....
}

public class UserMetaData
{
    [Required]
    [RegularExpression("[a-zA-Z]{2,30}")]
    public string FirstName { get; set; }
    ....
    ....
0

精彩评论

暂无评论...
验证码 换一张
取 消