asp.net-mvc 从模型中读取数据并在剃刀视图上显示 - mvc

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25535484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:12:57  来源:igfitidea点击:

Read data from model and display on razor view - mvc

asp.net-mvcrazor

提问by Newbie

I am trying to get the data from database through model and display it on a view. Here is the sample code, i am able to pull the data but that is not showing up on the view.I appreciate any help. Thanks in advance

我正在尝试通过模型从数据库中获取数据并将其显示在视图上。这是示例代码,我能够提取数据,但没有显示在视图中。感谢您的帮助。提前致谢

Model Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

namespace Test.Models
{
    public class CustModel
    {
        SqlConnection con = new SqlConnection();        
        List<CustModel> CustList = new List<CustModel>();       
        public String CustName { get; set; }        
        public String CustPhone { get; set; }         
        CustModel p = null;        
        public List< CustModel > GetCustInfo()        
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CustConnectionString"].ToString());
            con.Open();
            using (con)            
            {
            SqlCommand cmd = new SqlCommand("select * FROM Cust_tb where ZIP = 37771", con);               
            SqlDataReader rd = cmd.ExecuteReader();               
            while (rd.Read())               
            {                   
                    p = new CustModel ();
                    p.CustName =Convert.ToString(rd.GetSqlValue(0));
                    p.CustPhone = Convert.ToString(rd.GetSqlValue(10));                    
                    CustList.Add(p);               
            }            
            }            
            return CustList;
    }
}
}

Controller Code

public ActionResult CustDisplay()
        {
            CustModel p = new CustModel();
            List<CustModel> Li = new List<CustModel>();
            Li = p. GetCustInfo (); 
            ViewData["CustInfo"] = Li; 
            return View("CustDisplay");
        }

View Code

@model Test.Models.CustModel

@{
    ViewBag.Title = "CustDisplay";
}

<!DOCTYPE html>
<head>
<title> Customer Information </title>
</head>
<body>
<div class="display-label">
Name
</div>
<div class="display-field">
@Html.DisplayFor(Model => Model.CustName)
</div>
<div class="display-label">
Phone
</div>
<div class="display-field">
@Html.DisplayFor(Model => Model. CustPhone)
</div>
</body>


</html>

回答by RandomShelly

Maybe I am over-simplifying this - but I think it is just because you didn't actually send or use your list in the view... I don't get my data the way you did - so I will assume that you are getting the data you want, and the returning list contains 1 to many rows of data in p (name and phone)

也许我过度简化了这一点 - 但我认为这只是因为你实际上没有在视图中发送或使用你的列表......我没有像你那样获取我的数据 - 所以我会假设你是获取你想要的数据,返回列表包含p中的1到多行数据(姓名和电话)

even though you pass your list in ViewData - you aren't assigning it in the view.. the best way to do this would be in your controller to call return View(Li);and not return View("CustDisplay");you are already in the view you want to call - so it will send the list into the @model. (There may be a way to assign the ViewData to the model in the view also) - but I pass the list in, so the data is bound...

即使你在 ViewData 中传递你的列表 - 你没有在视图中分配它..最好的方法是在你的控制器中调用return View(Li);而不是return View("CustDisplay");你已经在你想要调用的视图中 - 所以它会发送列表到@model。(也可能有一种方法可以将 ViewData 分配给视图中的模型)-但我将列表传入,因此数据已绑定...

You should make the model @model IEnumerable<Test.Models.CustModel>

你应该做模型 @model IEnumerable<Test.Models.CustModel>

and then

进而

<table class="table">
                <tr>
                    <th>
                        @Html.DisplayNameFor(model => model.CustName)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.CustPhone)
                    </th>                        
                </tr>

                @foreach (var item in Model){
                <tr>   
                    <td>
                        @Html.DisplayFor(modelItem => item.CustName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.CustPhone)
                    </td>                    
               </tr>
            }
       </table>