English 中文(简体)
MVC 3 ,ajax,在主计长的行动中不作模拟约束
原标题:MVC 3 with ajax not doing ModelBinding in Controller action

我的MVC 3号控制器行动不是淡化我的AJAX哨所的JSON数据。

  • Fiddler shows the data being passed correctly.
  • I put a break point on the var x = "ok!"; line and it hits it every time.
  • When the contentType is removed from the .ajax(), the address object is created but properties are null values.
  • With the contentType in the request, the address object is null.
  • I ve tried to put [DataContract] and [DataMembers] on my POCO, no difference
  • I ve tried to use an IModelBinder, but the StreamReader(Request.InputStream).ReadToEnd was always ""

这里的《家庭法典》:

 $("#home-validate-btn").click(function (event) {
    var address =
    {
        Address: $( #Data_HomeAddress ).val(),
        City: $( #Data_HomeCity ).val(),
        State: $( #Data_HomeState ).val(),
        Zip: $( #Data_HomeZip ).val()
    };

    $.ajax({
        url:  /Settings/addressValidate ,
        type:  POST ,
        contentType:  application/json; charset=utf-8 , 
        dataType:  json ,
        data: $.toJSON(address),            
        success: function (info) {
            alert( ok! );
        }
    });
});

这里的《控制器法》:

 [AcceptVerbs(HttpVerbs.Post)]         
    public ActionResult addressValidate(ValidateAddress address)
    {
        var x = "ok!";
        return new JsonResult()
        {
            Data = (x),
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };
    }

My POCO:

public class ValidateAddress
{        
    public string Address { get; set; }        
    public string City { get; set; }        
    public string State { get; set; }   
    public string Zip { get; set; }
}

My Global.asax.cs

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}", // URL with parameters
            new
            {
                controller = "Home",
                action = "Index"
            } // Parameter defaults
        );
    }

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
    }

这里的数据来自菲德尔:

  • POST http://localhost.:59872/Settings/addressValidate HTTP/1.1
  • x-requested-with: XMLHttpRequest
  • Accept-Language: en-us
  • Referer: http://localhost.:59872/Settings/Addresses?Length=8
  • Accept: application/json, text/javascript, */*; q=0.01
  • Content-Type: application/json; charset=utf-8
  • Accept-Encoding: gzip, deflate
  • User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; OfficeLiveConnector.1.5; OfficeLivePatch.1.3; .NET4.0C; .NET4.0E; Creative AutoUpdate v1.40.01; msn OptimizedIE8;ENUS)
  • Host: localhost.:59872
  • Content-Length: 77
  • Connection: Keep-Alive
  • Pragma: no-cache
  • Cookie: __RequestVerificationToken_Lw__=IBBY7VzoqxMI .... (rest of string snipped)
  • ASP.NET_SessionId=fsjywpn4gqasszgcdgmkqd4p
  • {"Address":"1 Main Street","City":"Beach City","State":"FL","Zip":"99999"}
最佳回答

问题是,我需要把返回数据放在一个称为“地址”的物体上,以便与方法定义保持一致。 我通过一个阵列,而不是一个带有特性的物体。

New JSON =

var addressObj = {
        address: {
            Address: $( #Data_HomeAddress ).val(),
            City: $( #Data_HomeCity ).val(),
            State: $( #Data_HomeState ).val(),
            Zip: $( #Data_HomeZip ).val()
        }
    };

in the .ajax() - data: $.toJSON(addressObj),

Senior JSON =

var address =
{
    Address: $( #Data_HomeAddress ).val(),
    City: $( #Data_HomeCity ).val(),
    State: $( #Data_HomeState ).val(),
    Zip: $( #Data_HomeZip ).val()
};

原有数据:toJSON(地址)

http://forums.asp.net/p/1642394/4252987.aspx#4252987“rel=” http://forums.asp.net/p/1642394/4252987.aspx#4252987

问题回答

暂无回答




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签