samedi 9 mai 2015

How to use partial views from different models in ASP.NET MVC 5?

Hi I am trying to code a simple blog with using ASP.NET MVC 5 Framework. I have done CRUD operations of Posts. I mean I can add new articles and manage them. When I wanted to add comments to articles, I stuck. Comments will be added to Details pages of articles. So I should add Create comment page to Details page.

I used Code First model and I have two models. Articles and Comments. I decided to use partial views to enter comments. But result is an error:

The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.Article but this dictionary requires a model item of type 'Blog.Models.Comment'. Comments can not be added. I created 2 PartialViews, one of them is _CreateComments PartialView and other one is _Index PartialView

Details View:

@model Blog.Models.Article

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

 <div>
<h4>Article</h4>
<hr />
<dl class="dl-horizontal">
    <dt>
        @Html.DisplayNameFor(model => model.Title)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Title)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Author)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Author)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.Date)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.Date)
    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.ArticleContent)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.ArticleContent)
    </dd>

</dl>
 </div>
 <div class="jumbotron">
  @Html.Partial("~/Views/Comments/_CreateComments.cshtml", new Blog.Models.Comment())
  @Html.Partial("~/Views/Comments/_Index.cshtml", new List<Blog.Models.Comment> { new Blog.Models.Comment() })
   </div>

_CreateComment PartialView

  @model Blog.Models.Comment
   @using (Html.BeginForm("Create")) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Comment</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CommentContent, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CommentContent, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.CommentContent, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
 }

<div>
@Html.ActionLink("Back to List", "Index")
</div>

_Index PartialView @model IEnumerable

 <table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.Date)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.CommentContent)
    </th>
    <th></th>
</tr>

 @foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Date)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.CommentContent)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.CommentId }) |
        @Html.ActionLink("Details", "Details", new { id=item.CommentId }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.CommentId })
    </td>
</tr>
 }

 </table>

Aucun commentaire:

Enregistrer un commentaire