【问题标题】:How to bind jsonresult to label text in mvc using javascript如何使用javascript将jsonresult绑定到mvc中的标签文本
【发布时间】:2019-02-06 11:21:13
【问题描述】:

我正在尝试将 jsonresult 数据绑定到来自数据库的标签文本,但它不起作用.. 我已经发布了脚本代码,它将获取下拉列表选择的值并传递给 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>

控制器代码

 [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);

        }

【问题讨论】:

  • Jsonresult 未绑定到标签
  • 你能在这里提供你的html和json结果吗?
  • 是的,这是 html
  • 控制器代码

标签: javascript model-view-controller jsonresult


【解决方案1】:

通常.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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 2014-05-29
    • 2019-03-16
    • 2016-12-30
    • 1970-01-01
    相关资源
    最近更新 更多