简体   繁体   English

如何使用javascript将jsonresult绑定到mvc中的标签文本

[英]How to bind jsonresult to label text in mvc using javascript

I'm trying to bind jsonresult data to label text which are from database but it won't work.. I have posted script code herewith it will get dropdownlist selected value and pass to actionresult and then result data pass as jsonresult 我正在尝试将jsonresult数据绑定到来自数据库的标签文本,但是它将无法工作。.我已经在此发布了脚本代码,它将获得dropdownlist选定的值并传递给actionresult,然后结果数据作为jsonresult传递

<script>
        function getOutletDetails() {
            debugger;
            var centreName = $("#ddl_Outlet").find("option:selected").text();

            $.ajax
                ({
                    url: "@Url.Action("OutletDetails", "AddTickets", new {area="Common"})",
                    type: 'POST',
                    datatype: 'application/json',
                    contentType: 'application/json',
                    data: JSON.stringify({ centreName: +centreName }),
                    success: function (result) {

                        $("#lblTelephone1").text(result.Telephone_01);                     

                    },
                    error: function () {
                        alert("Whooaaa! Something went wrong..")
                    },
                });
        }

    </script>

Controller Code 控制器代码

 [HttpPost]
        public ActionResult OutletDetails(string centreName)
        {

            List<tbl_Centre> lstcity = new List<tbl_Centre>();
          //  int id1 = Convert.ToInt32(id);
            //ViewBag.Id = id;
            lstcity = (objbs.CentreBs.GetAll().Where(x => x.CentreName == centreName)).ToList<tbl_Centre>();

            var result = JsonConvert.SerializeObject(lstcity, Formatting.None, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });


            return Json(result, JsonRequestBehavior.AllowGet);

        }

Usually .text() will work like this 通常.text()会像这样工作

 $('#sampleButton').click(function(){ $('#SampleId').text($('.userInput').val()); }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label id="SampleId">Sample text</label><br><br> <input class="userInput" style="width: 300px" type="text" value="new value that will be replace at label"><br><br> <button id="sampleButton">change label text</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM