开发者

Passing data from Controller to View using JSon object

开发者_Go百科

I have the following to get the Json abject passed from the controller and populate the various textboxes in the view. However, nothing is happening even though controller is passing a valid Json object. What is wrong with this code???

<script language="javascript" type="text/javascript">

 $(document).ready(function() {
     var url = '<%=Url.Action("DropDownChange") %>';
     $("#vendorID").change(function() {
         var selectedID = $(this).val();
         if (selectedID != "New Vendor Id") {
             //$.post('Url.Action("DropDownChange","Refunds")', function(result) {
             $.post(url, { dropdownValue: selectedID }, function(result) {
                 alert(selectedID);
                 $("#name").val(result.Name);
                 $("#city").val(result.City);
                 $("#contact").val(result.Contact);
                 $("#address2").val(result.Address2);
                 $("#address1").val(result.Address1);
                 $("#state").val(result.State);
                 $("#zip").val(result.Zip);

             });

         }
     });

 });

This is the code in my controller;

public JsonResult DropDownChange(string dropdownValue)
    // This action method gets called via an ajax request   
    {


        if (dropdownValue != null && Request.IsAjaxRequest() == true)
        {

            paymentApplicationRefund  =
            cPaymentRepository.PayableEntity(dropdownValue);

            paymentApplicationRefund.Address1.Trim();
            paymentApplicationRefund.Address2.Trim();
            paymentApplicationRefund.Name.Trim();
            paymentApplicationRefund.City.Trim();
            paymentApplicationRefund.Contact.Trim();
            paymentApplicationRefund.State.Trim();
            paymentApplicationRefund.Zip.Trim();

            return Json(paymentApplicationRefund,"application/json");               
        }

        else
        {
            return null;
        }
    }


You probably just need to tell it to expect JSON data back. By default it assumes it's HTML.

$.post(url, { dropdownValue: selectedID }, function(result) {
              alert(selectedID);
              $("#name").val(result.Name);
              $("#city").val(result.City);
              $("#contact").val(result.Contact);
              $("#address2").val(result.Address2);
              $("#address1").val(result.Address1);
              $("#state").val(result.State);
              $("#zip").val(result.Zip);
           }, 'json'); 


I prefer sending Json to a ActionResult with my DTO as the parameter and use the JsonValueProviderFactory do the deserialization for me.

Sending JSON to an ASP.NET MVC Action Method Argument


Try this... Add the ".change()" at the end of the function.

$(document).ready(function() {      
   var url = '<%=Url.Action("DropDownChange") %>';
   $("#vendorID").change(function() {          
   .....
   }).change();
}); 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜