1

I am new to asp.net mvc . This is how my model looks like:

 [Bind(Exclude = "JournalId")]
public class Journal
{
    [ScaffoldColumn(false)]
    public int JournalId { get; set; }

    [DisplayName("Customer")]
    public int CustomerId { get; set; }

    [DisplayName("Till")]
    public int TillId { get; set; }

    [Required(ErrorMessage = "A Journal name is required")]
    [StringLength(160)]
    public string Name { get; set; }

    [DisplayName("Journal creation date")]
    public DateTime Date { get; set; }

    [DisplayName("Journal creation time")]
    public DateTime Time { get; set; }

    public virtual Customer Customer { get; set; }
    public virtual Till Till { get; set; }

}

[Bind(Exclude = "CustomerId")]
public class Customer
{
    [ScaffoldColumn(false)]
    public int CustomerId { get; set; }

    [Required(ErrorMessage = "A customer name is required")]
    [StringLength(160)]
    public string Name { get; set; }

    [StringLength(250)]
    public string Address { get; set; }
}


[Bind(Exclude = "TillId")]
public class Till
{
    [ScaffoldColumn(false)]
    public int TillId { get; set; }

    [Required(ErrorMessage = "A till no is required")]
    [StringLength(160)]
    public string TillNo { get; set; }

    [StringLength(100)]
    public string TillOperator { get; set; }
}

This is how my one of my controller's action is defined:

 public ViewResult Index()
    {
        var journals = db.Journals.AsEnumerable<Journal>();
        ViewData["journals"] = journals;
        return View();
    }

and the view :

@model IEnumerable<ErikDemo.Models.Journal>
@foreach (var item in (IEnumerable<ErikDemo.Models.Journal>)ViewData["journals"]) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Customer.Name)
    </td>
    <td>
        @Truncate(item.Till.TillNo, 25)
    </td>
    <td>
        @Truncate(item.Name, 25)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Date)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Time)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.JournalId }) |
        @Html.ActionLink("Details", "Details", new { id=item.JournalId }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.JournalId })
    </td>
</tr>

Although when I am debugging I can see in the controller that the list passed to the View is not empty, and also I see that the ViewData["journals"].Local in a watch is not empty, nothing gets displayed. I have also used the View.Model and return View(journals.ToList()) to send data to the View, but nothing seems to work. What is the issue here? Been on that half a day.

2 Answers 2

2

This is wrong: (Well it can be done like this, but I think you want to pass a model)

public ViewResult Index()
{
    var journals = db.Journals.AsEnumerable<Journal>();
    ViewData["journals"] = journals;
    return View();
}

Try this:

public ViewResult Index()
{
    var journals = db.Journals.AsEnumerable<Journal>();

    return View(journals); //You just passed journals as a model
}

Also if you are using mvc 3 you can use ViewBag instead of ViewData

Example:

ViewData["Journal"] = "my string";

is the same as

ViewBag.Journal = "my string";

The ViewBag is dynamic, so you can use dot notation.

Additionally

This code:

@model IEnumerable<ErikDemo.Models.Journal>
@foreach (var item in (IEnumerable<ErikDemo.Models.Journal>)ViewData["journals"])

Should be like this:

@model IEnumerable<ErikDemo.Models.Journal>
@foreach (var item in Model)

Update:

I'm not sure what you're doing with this db.Journals.AsEnumerable<Journal>();

You should have a method somewhere that gets data from a table or table(s) and returns Journals. So lets say this all comes from one table in a database:

public class JournalViewModel
{
    public IEnumerable<Journals> GetJournals()
     {
         using(var db = new ErikDataContext())
          {
              return db.Journals.ToList();

          }
     }
}

Then in the action:

public ViewResult Index()
{
    var journals = new JournalsViewModel.GetJournals();

    return View(journals); //You just passed journals as a model
}
Sign up to request clarification or add additional context in comments.

4 Comments

Well I tried it and it does not seem to work. The data exists in Model, I can see it with the debugger, but it "refuses" to iterate through it which is puzzling...
In your index action within your controller try doing journal.ToList() when you pass it as a model.
Where does the variable db come from? Let me update my answer.
the db is a DbContext which has the following properties : public DbSet<Journal> Journals { get; set; } public DbSet<Customer> Customers { get; set; } public DbSet<Till> Tills { get; set; }.The issue was that if you call db.Journals is as IQueriable and when converting to IEnumerable it sort of fails.so if you want to have access to the entities you have to call db.Journals.Local.Changed it to that and It worked.However a little unsure why ...
1

Did you forget the <table> tag? If you haven't viewed the source of your page as it is rendered, I would recommend that you do this as a next step.

2 Comments

No , It was just a code snippet.It does have the <table> tag.
Is there any output at all in the rendered HTML? This is not clear from your question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.