【发布时间】:2020-10-13 20:43:55
【问题描述】:
我正在使用实体框架,但问题是州名、城市名和图像未显示索引页面
查看输出:
我想在索引页上打印州名、城市名和图片?
HomeController.cs
public class HomeController : Controller
{
private readonly InsAjaxEntities InsAjaxEntities;
public HomeController()
{
InsAjaxEntities = new InsAjaxEntities();
}
public ActionResult Index()
{
return View();
}
public JsonResult Getdata()
{
return Json(InsAjaxEntities.students.ToList(), JsonRequestBehavior.AllowGet);
}
索引.cshtml
@{
ViewBag.Title = "Index";
}
<div class="container">
<h2>List Of Student</h2>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>
StudentID
</th>
<th>
StudentName
</th>
<th>
StudentAddress
</th>
<th>
StudentClass
</th>
<th>
StudentAge
</th>
<th>
StudentImage
</th>
<th>
Gender
</th>
<th>
Stateid
</th>
<th>
Cityid
</th>
<th>
Pincode
</th>
<th>
StudentHobby
</th>
</tr>
</thead>
<tbody class="tbody"></tbody>
</table>
</div>
@section scripts {
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
loadData();
});
function loadData() {
$.ajax({
url: "@Url.Action("Getdata")",
type: "GET",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (result) {
var html = '';
$.each(result, function (key, item) {
html += '<tr>';
html += '<td>' + item.studentid + '</td>';
html += ' <td>' + item.studentname + '</td>';
html += '<td>' + item.studentaddress + '</td>';
html += '<td>' + item.studentclass + '</td>';
html += '<td>' + item.studentage + '</td>';
html += '<td>' + item.studentimage + '</td>'; //here I want to displaing the image
html += '<td>' + item.gender + '</td>';
html += '<td>' + item.stateid + '</td>'; //here I want to print the statename
html += '<td>' + item.cityid + '</td>'; //here I want to print the cityname
html += '<td>' + item.pincode + '</td>';
html += '<td>' + item.studenthobby + '</td>';
html += '</tr>';
});
$('.tbody').html(html);
},
error: function (errormessage) {
alert(errormessage.responseText);
}
});
}
</script>
}
<a href="@Url.Action("Create", "Home")" class="elements"><span>Create</span></a>
如何打印州名和城市名并在索引页上显示图像?
现在将 stateid 和 city id 存储在表中,但是如何打印 statename 和 cityname?
帮助?
【问题讨论】:
标签: javascript html jquery ajax asp.net-mvc