I am developing a Blog application wherein users can post mutliple posts for a blog using model first approach in MVC3.
I have created a blog with Edit, Details, Delete and Posts for Post feature I am trying to post to a blog, however I'm facing a challenge in doing so. Getting the below error when I click on Post.
"The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.Post_B87C75979DC030A3A554DA135032EB32E63C7DCD0D5CC9E499F8BD62FCF57923', but this dictionary requires a model item of type 'App.Models.Blog'."
//PostController
public class PostController : Controller
{
WWAppModelContainer db = new WWAppModelContainer();
public ActionResult Index([Bind(Prefix = "id")] int BlogId) //[Bind(Prefix = "id")] int BlogId
{
var blog = db.Posts.Find(BlogId);
return View(blog);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
//BlogController
public class BlogController : Controller
{
private WWAppModelContainer db = new WWAppModelContainer();
//
// GET: /Blog/
public ActionResult Index()
{
return View(db.Blogs.ToList());
}
//
// GET: /Blog/Details/5
public ViewResult Details(int id)
{
Blog blog = db.Blogs.Find(id);
if (blog == null)
{
return View("Not found");
//return HttpNotFound();
}
return View(blog);
}
//
// GET: /Blog/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Blog/Create
[HttpPost]
public ActionResult Create(Blog blog)
{
if (ModelState.IsValid)
{
db.Blogs.Add(blog);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(blog);
}
//
// GET: /Blog/Edit/5
public ActionResult Edit(int id)
{
Blog blog = db.Blogs.Find(id);
return View(blog);
}
//
// POST: /Blog/Edit/5
[HttpPost]
public ActionResult Edit(Blog blog)
{
if (ModelState.IsValid)
{
db.Entry(blog).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(blog);
}
//
// GET: /Blog/Delete/5
public ActionResult Delete(int id)
{
Blog blog = db.Blogs.Find(id);
return View(blog);
}
//
// POST: /Blog/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Blog blog = db.Blogs.Find(id);
db.Blogs.Remove(blog);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
//Blog -->Index.cshtml
@model IEnumerable<App.Models.Blog>
@{
ViewBag.Title = "Index";
}
<h2>
Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Title
</th>
<th>
BloggerName
</th>
<th>
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.BloggerName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Post", "Index", "Post", new { id = item.Id }, null) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
//Post -->Index.chtml
@model App.Models.Blog
@{
ViewBag.Title = "Index";
}
<h2>Posts for @Model.Posts</h2>
@Html.Partial("_Posts",@Model.Posts)
<p>
@Html.ActionLink("Create New", "Create")
</p>
//Post-->_Posts.cshtml
@model IEnumerable<App.Models.Post>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.DateCreated)
</th>
<th>
@Html.DisplayNameFor(model => model.Content)
</th>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateCreated)
</td>
<td>
@Html.DisplayFor(modelItem => item.Content)
</td>
<td>
@Html.DisplayFor(modelItem => item.Blog.Title)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@* @Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })*@
</td>
</tr>
}
</table>