0

I am learning Entity Framework and MVC.

This is my model:

    public class ChatLogContext : DbContext
{
    public ChatLogContext()
        : base("connString")
    {
    }

    public DbSet<ChatLogs> ChatLogs { get; set; }
}

[Table("ChatLogs")]
public class ChatLogs
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ChatLogId { get; set; }
    [Column("Message")]
    public string Message { get; set; }
    [Column("UserId")]
    public int UserId { get; set; }

}

And this is my controller code here:

        public ActionResult Index()
    {

        using(var db = new ChatLogContext())
        {
            var list = db.ChatLogs.Select(p => p.Message).SingleOrDefault();
            ViewBag.data = list;
            return View();

        }


    }

I then access that data in view like this:

@model Chat.Models.ChatLogs
@Html.Raw(ViewBag.data)

I can access 1 record as seen here with this.

But I would like to learn, how to access all records from the table ChatLogs with Entity Framework and pass it to view with Razor method(foreach), so I can format that data (I don't like default tables that VS generates). I am now using ViewBag for one row and 1 column, this is the most far I came.

I just can't find an examples on Google that would help my brains.

Help appreciated.

PS: Is it better to work with pure entity or mix linq(linq to entities)?

tereško
  • 58,060
  • 25
  • 98
  • 150
sensei
  • 7,044
  • 10
  • 57
  • 125
  • please explain what you mean by "PS: Is it better to work with pure entity or mix linq(linq to entities)?". – Aron Aug 05 '13 at 03:11
  • Using entity to query or linq to entity? – sensei Aug 05 '13 at 11:46
  • Sorry. I still do not understand what you mean by "Entity to Query" and "Linq to Entity". Perhaps you should edit your question to provide examples of each in code. Even better, open up a new question on Programmers (since it sounds like a question on code quality). – Aron Aug 06 '13 at 01:52

1 Answers1

0

Typically the Index action is for showing a grid of all the entities (in this case ChatLogs).

One of the points of the Razor View Engine is that you get typed Views. So typically I would pass the data to the view directly as opposed to using the ViewBag.

public ActionResult Index()
{

    using(var db = new ChatLogContext())
    {
        var list = db.ChatLogs.ToList();
        return View(list);

    }
}

The next step is to have the View typed to IEnumerable<ChatLog>. Visual Studio should help you with that. Then you can just foreach over the ChatLogs.

Aron
  • 15,464
  • 3
  • 31
  • 64
  • I tried it like that before, and it gave me an error: "The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Chat.Models.ChatLogs]'." So I made it working like this: http://pastebin.com/zdKFR0pZ .. But the since I changed the View type to IEnumerable I can't use strongly typed views anymore. This for example doesn't work: @*Html.TextBoxFor(m => m.Message, new { id = "txtMsg" }) *@ It's not a problem I can use TextBox, but still understand?.. – sensei Aug 05 '13 at 04:05
  • My bad...removed the `Select(x => x.Message)`, now good old polymorphism should work. You shouldn't need to specify the id. As for how to use the TextBoxFor stuff... Its a little annoying if you want to have 'two way binding' like behavior. You need to introduce an interator, use `@Model List` and use `@Html.TextBoxFor(x => x[i].Message)` not nice...however I typically find I never NEED that. Since for large data sets I only have to display (reduce the postback size) http://stackoverflow.com/a/11261698/1808494. – Aron Aug 05 '13 at 04:19
  • I am asking because if you do not use `@Html.TextBoxFor(x => x.Message)` you don't have to serialize data on post, if you use textbox only, you just post it with json, give direct values { value = $("#name_ofTextbox").val() }.. if thats all that changes then np. 1 more question. how could I append this data you get from controller to textarea in mvc? it was easy in webforms. thanks – sensei Aug 05 '13 at 11:54
  • I mean is there other way than jQuery/Js? Because if I will probably have to foreach through jquery.append to add it. Are we using jQuery particualy with this things in mvc views or? – sensei Aug 05 '13 at 12:39
  • You don't NEED jQuery anywhere here. However. Consider, when you have 1000 objects in the database. You would pull out all 1000 objects to display on the webpage. Then on post you send ALL 1000 objects back. Consider the bandwidth. Yes you can do it. No I wouldn't do it like that. (if you do wnat to do it, use a list and `@Html.TextBoxFor(x => x[i].Message)` for your TextBoxes). – Aron Aug 06 '13 at 01:55