when get ajax data from url then i want show data in Html form
Json Data
[
{
"Buildings": {
"Campuses": {
"ID": 3,
"Campus_Name": "Dhaka"
},
"ID": 9,
"Building_Name": "D",
"CampusID": 3
},
"Campuses": {
"ID": 3,
"Campus_Name": "Dhaka"
},
"ID": 17,
"Floor_Name": "1st",
"CampusID": 3,
"BuildingID": 9
}
]
Html
<form id="form">
@Html.TextBoxFor(m => m.ID, new { @id = "Floor_ID", @class = "form-control", @placeholder = "Floor_ID**" })
<div class="form-group">
@Html.LabelFor(model => model.Floor_Name, htmlAttributes: new { @class = "col-form-label" })
@Html.TextBoxFor(m => m.Floor_Name, new { @id = "FloorName", @class = "form-control", @placeholder = "Name*" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.CampusID, htmlAttributes: new { @class = "col-form-label" })
@Html.DropDownListFor(m => m.CampusID, ViewBag.CampusID as SelectList, new { @class = "form-control", @id = "CampusID" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.BuildingID, htmlAttributes: new { @class = "col-form-label" })
<select id="BuildingID" name="BuildingID" class="form-control">
<option value="" >---Select---</option>
</select>
</form>
2
Answers
Try something like this!! for the js we have
/////////////////////////////////////////////////for the html, you can have
If what you really want is to show the data, you can do this, given the html (cshtml) above
//use this to parse the json string as object
var data = JSON.parse(THE-JSON-STRING);
//use this to target the elements you want
document.getElementById(“FloorName”).value = data.Floor_Name;
document.getElementById(“CampusID”).value = data.CampusID;
document.getElementById(“BuildingID”).value = data.BuildingID;