I am working with entity framework but issue is statename,cityname and image not displaying the index page
see output:
I want to print the statename,cityname and image on the index page?
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);
}
Index.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>
how to print the statename and cityname and displaying the image on index page?
now store the stateid and city id in the table but how to print statename and cityname?
help?
2
Answers
You need to return a view model that joins
student
,state
andcity
tables instead ofInsAjaxEntities.students
Have a look at this article: https://entityframework.net/joining
Use Below code sample to achieve your goal and below points mention how you will use this code sample.
Use this code sample to get the data with above mention
tables
incontext
. As of now i just put the sample column name andtable
name use your actualcolumn name
andtable name
.Then create a
ViewModel
which contain all the fields which is return in above Var.Then Pass this
ViewModel
onView
and bind.