![](/img/trans.png)
[英]How to bind text to the label which is in JsonObj using jquery
[英]How to bind jsonresult to label text in mvc using javascript
我正在嘗試將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>
控制器代碼
[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);
}
通常.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.