I have merged two views in one view(one to create and the other one to display).So the new view will be able to add and retrieve data in the same time. but it was only able to create a new row in the database without retrieve anything. There is 4 rows in the database.
here is my new view (Index): i added h1> Works/h1> to make sure foreach works
Index.cshtml
@model ReMvc.ViewModel.MovieViewModel
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Movie</legend>
<div class="editor-label">
@Html.LabelFor(movie => movie.Movie.Title)
</div>
<div class="editor-field">
@Html.EditorFor(movie => movie.Movie.Title)
@Html.ValidationMessageFor(movie => movie.Movie.Title)
</div>
<div class="editor-label">
@Html.LabelFor(movie => movie.Movie.ReleaseDate)
</div>
<div class="editor-field">
@Html.EditorFor(movie => movie.Movie.ReleaseDate)
@Html.ValidationMessageFor(movie => movie.Movie.ReleaseDate)
</div>
<div class="editor-label">
@Html.LabelFor(movie => movie.Movie.Genre)
</div>
<div class="editor-field">
@Html.EditorFor(movie => movie.Movie.Genre)
@Html.ValidationMessageFor(movie => movie.Movie.Genre)
</div>
<div class="editor-label">
@Html.LabelFor(movie => movie.Movie.Price)
</div>
<div class="editor-field">
@Html.EditorFor(movie => movie.Movie.Price)
@Html.ValidationMessageFor(movie => movie.Movie.Price)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
foreach (var item in Model.MovieCollection)
{
<tr>
<h1>Works</h1>
<td>
@Html.Display(item.Title)
</td>
<td>
@Html.Display(item.ReleaseDate.ToString())
</td>
<td>
@Html.Display(item.Genre)
</td>
<td>
@Html.Display(item.Price.ToString())
</td>
</tr>
}
}
<p>
</p>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
My problem that from the above code foreach doesn't retrieve data from MovieCollection to index page. Here is my model (Movie):
using System;
using System.Data.Entity;
namespace ReMvc.Models
{
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}
public class MovieDbContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
}
}
And here is a new type to solve an issue:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ReMvc.ViewModel
{
public class MovieViewModel
{
public ReMvc.Models.Movie Movie { get; set; }
public IEnumerable<ReMvc.Models.Movie> MovieCollection { get; set; }
}
}
Here is my controller:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ReMvc.Models;
using ReMvc.ViewModel;
namespace ReMvc.Controllers
{
public class MoviesController : Controller
{
private MovieDbContext db = new MovieDbContext();
//
// GET: /Movies/
public ActionResult Index()
{
var model = new MovieViewModel()
{ MovieCollection = db.Movies.ToList(), };
return View(model);
}
//
// POST: /Movies/Index
[HttpPost]
public ActionResult Index(Movie movie)
{
if (ModelState.IsValid)
{
db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
var model = new MovieViewModel()
{
Movie = movie,
};
return View(movie);
}
}
1. add the Enumerable list of row to use it with foreach, in the model in the entity folder:
2. edit the home controller, to make it deal with httpPost and httpGet requests
3.edit the index page to contain the two views, one for adding and the second to retrieve , as below:
@
@In your post action, you're not setting a value for
MovieCollection
on your view model. This information doesn't survive the request, and you're not even accepting the full view model from the post, if it did. As a result, you simply need to repopulate the list in your post action before returning the view.