【问题标题】:how to display the data in mvc?如何在mvc中显示数据?
【发布时间】: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


    【解决方案1】:

    您需要返回一个连接studentstatecity 表的视图模型,而不是InsAjaxEntities.students

    看看这篇文章:https://entityframework.net/joining

    【讨论】:

      【解决方案2】:

      使用下面的代码示例来实现您的目标,以下几点提到了您将如何使用此代码示例。

      var users = (from stu in DbContextName.StudentTableName 
                                              Join sta in DbContextName.StateTable on stu.stateid equals sta.stateid 
                                              Join cit in DbContextName.CityTable on sta.cityid equals sta.cityid                             
                                   select new {
                                       stu.studentColumn1, stu.studentColumn2, stu.studentColumn3, stu.studentColumn4, stu.studentColumn5,
                                       sta.stateColumn1, sta.stateColumn2,
                                       cit.cit.citycolumn1, cit.citycolumn1
                                   }).ToList();
      

      使用此代码示例在context 中获取上面提到的tables 的数据。到目前为止,我只是将示例列名称和table 名称使用您的实际column nametable name。 然后创建一个ViewModel,其中包含上面 Var 中返回的所有字段。 然后将这个ViewModel 传递给View 并绑定。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-10
        • 2020-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多