In View "Index" output list of users, their names, messages date, messages. And near with every have link "Info". How to display in View "Info" List<string> ListMessage
of user after click on link "Info"? Output only is empty table :(
ASP.NET is a hardcore.
This is my Model :
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; }
}
My Controller:
[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);
}
And View "Info":
@{ 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>
You did not init values for ListMessage. I tried to init 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);
}
It show Test1 and Test2 value in the table.
Also not sure why you assign 2 ViewBags without using them?
May be you want c.ListMessage = db.Users.Select(c=>c.Message).ToList()
?
It looks like you want to get the user matching an id
. You also need to populate user.ListMessage
You could try this:
[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);
}
Then the table should be populated in the view using the foreach
loop.
If you prefer, you could return a user
to the Info
view. In that case you can change to:
[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);
}
Then in the view update the foreach
to something like this:
@{
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
TY all for help!
@haldo Why did you deleted your message? You helped me! Thx!
Model:
[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);
}
View:
@{ 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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.