如何在视图列表中显示<字符串>

时间:2019-03-18 20:12:06

标签: c# asp.net-mvc

在视图“索引”中输出用户列表,他们的姓名,消息日期,消息。并且附近每个都有链接“ Info”。 单击链接“信息”后如何在用户的视图“信息” List<string> ListMessage中显示? 仅输出为空表:(

ASP.NET是硬核。

这是我的模型

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Message { get; set; }
    public List<string> ListMessage { get; set; }
    public DateTime Date { get; set; }
}

我的控制器:

[HttpPost]
    public ActionResult Index(User user)
    {
        IEnumerable<User> users = db.Users;
        if (user.Name == users.Where(i => i.Name == user.Name).ToString())
        {
            user.ListMessage = new List<string>();
            user.ListMessage.Add(user.Message);          
        }
        user.ListMessage = new List<string>();
        user.ListMessage.Add(user.Message);
        ViewBag.Customers = users;
        user.Date = DateTime.Now;
        db.Users.Add(user);
        db.SaveChanges();            
        return View();
    }
    [HttpGet]
    public ActionResult Info(int id)
    {
        IEnumerable<User> users = db.Users;
        ViewBag.Customers = users;
        var c = new User();
        c.ListMessage = new List<string>();
        ViewBag.Messages = c.ListMessage;
        return View(c.ListMessage);
    }

查看“信息”:

@{
    ViewBag.Title = "All messages";
}
@using MessageBank.Models
@model List<string>
        <table>               
            <tr>
                <td><p>Id</p></td>
                <td><p>Name</p></td>
                <td><p>Time</p></td>
                <td><p>Message</p></td>
                <td></td>
            </tr>
            @foreach (var el in Model)
            {
            <tr>
                <td><p>@Html.DisplayFor(m => el)</p></td>
                <td><p>@el</p></td>
            </tr>
            }
        </table>

3 个答案:

答案 0 :(得分:1)

您没有初始化ListMessage的值。 我试图初始化c.ListMessage = new List<string> { "Test1", "Test2" };

 [HttpGet]
    public ActionResult Info(int id)
    {
        IEnumerable<User> users = db.Users;
        ViewBag.Customers = users;
        var c = new User();
        c.ListMessage = new List<string> { "Test1", "Test2" };
        ViewBag.Messages = c.ListMessage;
        return View(c.ListMessage);
    }

它在表中显示Test1和Test2的值。

还不确定为什么要分配2个ViewBag而不使用它们吗?

也许您想要c.ListMessage = db.Users.Select(c=>c.Message).ToList()吗?

答案 1 :(得分:0)

您似乎想让用户与id相匹配。您还需要填充user.ListMessage,您可以尝试以下操作:

[HttpGet]
public ActionResult Info(int id)
{
    // get user with user.Id == id
    User user = db.Users.FirstOrDefault(u => u.Id == id);

    if (user == null)
    {
        // handle null
    }

    // populate listmessage
    user.ListMessage = new List<string> { user.Message };

    // send a List<string> to view
    return View(user.ListMessage);
}

然后应该使用foreach循环在视图中填充表。


如果愿意,可以将user返回到Info视图。在这种情况下,您可以更改为:

[HttpGet]
public ActionResult Info(int id)
{
    User user = db.Users.FirstOrDefault(u => u.Id == id);    

    // populate ListMessage
    user.ListMessage = new List<string> { user.Message };

    // return User model to view    
    return View(user);
}

然后在视图中将foreach更新为如下内容:

@{
    ViewBag.Title = "All messages";
}

@model YourNamespace.Models.User

    <table>   
        <tr>
            <th>Message</th>     
        <tr>           

        @foreach (var el in Model.ListMessage)
        {
            <tr>
                <td>@Html.DisplayFor(m => el)</td>
            </tr>
        }
    </table>

   // you can access Model.Id, Model.Name, Model.Date, Model.Message in this view too

答案 2 :(得分:0)

TY寻求帮助!

@haldo为什么删除了您的消息?你帮了我!谢谢!

型号:

[HttpGet]
    public ActionResult Info(int id)
    {
        User user = db.Users.FirstOrDefault(u => u.Id == id);
        user.ListMessage = new List<string>() { user.Message };
        return View(user.ListMessage);
    }

查看:

@{
    ViewBag.Title = "All messages";
}
@using MessageBank.Models
@model IEnumerable<string>

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Info</title>
</head>
<body>
    <div>
        <h3>Info</h3>
        <table>        
            <tr>
                <td><p>Message</p></td>
            </tr>
            @foreach (var el in Model)
            {
                <tr>
                    <td><p>@Html.DisplayFor(m => el)</p></td>
                    @*<td><p>@el</p></td>*@
                </tr>
            }
        </table>
        @foreach (var el in Model)
        {
            <ul>
                <li><p>@Html.DisplayFor(m => el)</p></li>
                @*<td><p>@el</p></td>*@
            </ul>
        }
    </div>
</body>
</html>