none
后端代码在js中接收不到。。 RRS feed

  • 问题

  • 官方费用:<label id="officalcharge" name="officalcharge"></label>元,
    服务费用:<label id="servicecharge" name="servicecharge"></label>元,
    市场价:<label id="marketcharge" name="marketcharge"></label>元

    [HttpPost]
            public JsonResult GetSubjectCharge(int subjectId)
            {
                if (subjectId == 0)
                    return null;
                var charge = CacheService.GetChargeBySubjectId(subjectId);
                return Json(new SubjectCharge {SubjectId = charge.SubjectId, OfficialCharge = charge.OfficialCharge, ServiceCharge = charge.ServiceCharge, MarketCharge = charge.MarketCharge });
            }

    function setSubjectCharge() {
        $.post("/agency/getsubjectcharge", { subjectId: $('input:radio:checked').val() }, function (data) {
            $("#officalcharge").val(data.OfficalCharge);
            $("#servicecharge").val(data.ServiceCharge);
            $("#marketcharge").val(data.MarketCharge);
        });
    }
    为什么3个label不能获取到数值
    2015年5月11日 9:23

答案

  • 你好,

    如果你想从前端调用后台代码,我建议你使用JQuery Ajax去调用后台的Web Mehod. 这里有一个示例,你可以参考它:

    Code Behind:

            [System.Web.Services.WebMethod]
            public static User SendParameters(int id)
            {
                User u1 = new User();
                u1.ID = id;
                u1.Name = "AAA";
                u1.Age = 23;
                return u1;
    
            }   
        public class User
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
    
        }

    Client Side:

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20130526/json2.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("[id*=btnSubmit]").click(function () {
                    var obj = {};
                    obj.id =1001 ;
                    $.ajax({
                        type: "POST",
                        url: "JqueryCallWebMethod.aspx/SendParameters",
                        data: JSON.stringify(obj),  //transfer data to code behind
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (r) {
                            var item = r.d;
                            $("#lblID").text(item.ID);
                            $("#txtName").val(item.Name);
                            $("#txtAge").val(item.Age)
                        }
                    });
                    return false;
                });
            });
        </script>
    
               <table border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td>ID</td>
                        <td>
                            
                            <asp:Label ID="lblID" runat="server" Text=""></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td>Name:
                        </td>
                        <td>
                            <asp:TextBox ID="txtName" runat="server" Text="" />
                        </td>
                    </tr>
                    <tr>
                        <td>Age:
                        </td>
                        <td>
                            <asp:TextBox ID="txtAge" runat="server" Text="" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Button ID="btnSubmit" Text="Submit" runat="server" />
                        </td>
                    </tr>
                </table>
    另外这里有篇博客,你可以参考它:http://www.mikesdotnetting.com/Article/96/handling-json-arrays-returned-from-asp-net-web-services-with-jquery

    2015年5月28日 8:49
    版主

全部回复

  • 你先看看发送和接受到的网络数据各是什么

    http://feiyun0112.cnblogs.com/

    2015年5月12日 3:03
    版主
  • 你好,

    如果你想从前端调用后台代码,我建议你使用JQuery Ajax去调用后台的Web Mehod. 这里有一个示例,你可以参考它:

    Code Behind:

            [System.Web.Services.WebMethod]
            public static User SendParameters(int id)
            {
                User u1 = new User();
                u1.ID = id;
                u1.Name = "AAA";
                u1.Age = 23;
                return u1;
    
            }   
        public class User
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
    
        }

    Client Side:

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20130526/json2.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("[id*=btnSubmit]").click(function () {
                    var obj = {};
                    obj.id =1001 ;
                    $.ajax({
                        type: "POST",
                        url: "JqueryCallWebMethod.aspx/SendParameters",
                        data: JSON.stringify(obj),  //transfer data to code behind
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (r) {
                            var item = r.d;
                            $("#lblID").text(item.ID);
                            $("#txtName").val(item.Name);
                            $("#txtAge").val(item.Age)
                        }
                    });
                    return false;
                });
            });
        </script>
    
               <table border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td>ID</td>
                        <td>
                            
                            <asp:Label ID="lblID" runat="server" Text=""></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td>Name:
                        </td>
                        <td>
                            <asp:TextBox ID="txtName" runat="server" Text="" />
                        </td>
                    </tr>
                    <tr>
                        <td>Age:
                        </td>
                        <td>
                            <asp:TextBox ID="txtAge" runat="server" Text="" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Button ID="btnSubmit" Text="Submit" runat="server" />
                        </td>
                    </tr>
                </table>
    另外这里有篇博客,你可以参考它:http://www.mikesdotnetting.com/Article/96/handling-json-arrays-returned-from-asp-net-web-services-with-jquery

    2015年5月28日 8:49
    版主