Pass a user defined object to ASP.NET Webmethod from jQuery, using JSON

后端 未结 2 1202
长情又很酷
长情又很酷 2020-12-01 08:24

I am trying to pass in some simple JSON to an ASP.NET 4.5 Webmethod from jQuery. And it is not working quite the way I want it. It works if I accept the inputs as separate

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 09:16

    I am not aware of ASP. But in Ruby on Rails we are using the below procedure. My form having nearly 20 fields. Through serializeArray(); I can send all the input field values to controller. Like

    Your HTML should be look like

    
    

    "name" field is mportant here.

    var form = $("form#driver_info_form").serializeArray();
    var hsh = { }
    $.each(form, function(i, e) {
                hsh[e.name] = e.value
            });
    
    var jqxhr = $.post("/app/DriverInfo/save_driver_info", {
                hsh: hsh
            }, function() { });
    

    On controller side we can get param like

    {"hsh"=>{"violation_date"=>"date", "violation_time"=>"time", "violation_day"=>"week", "username"=>"name", "address"=>"address", "city"=>"city", "state"=>"state", "zip"=>"", "driver_lic_number"=>"123324", "radio_commercial"=>"Yes", "age"=>"", "birth_date"=>"", "sex"=>"", "hair"=>"", "eyes"=>"", "height"=>"", "weight"=>"", "race"=>""}, "accident_check"=>"false", "misdemeanor"=>"false", "traffic"=>"false", "non_traffic"=>"false", "commercial"=>"Yes"}
    

    From this we can access the values.

    I hope this will give some guideline to you.

提交回复
热议问题