Ahmado
Ahmado

Reputation: 58

ASP.NET MVC I'm trying to add a review product (http post) in the same page using (partial view)

_getProductReviews.cshtml:

I'm calling my partial view like this:

<p>@Html.Partial("_CreateR");</p>

_CreateR.cshtml:

This code is auto generated by the controller:

@model Commerce.Domain.Entites.t_review

@using (Html.BeginForm())

{

@Html.AntiForgeryToken()

<div class="form-horizontal">

    <h4>t_review</h4>

    <hr />

    @Html.ValidationSummary(true)

    <div class="form-group">

        @Html.LabelFor(model => model.text, new { @class = "control-label col-md-2" })

        <div class="col-md-10">

            @Html.EditorFor(model => model.text)

            @Html.ValidationMessageFor(model => model.text)

        </div>

    </div>

    <div class="form-group">

        @Html.LabelFor(model => model.title, new { @class = "control-label col-md-2" })

        <div class="col-md-10">

            @Html.EditorFor(model => model.title)

            @Html.ValidationMessageFor(model => model.title)

        </div>

    </div>

    <div class="form-group">

        @Html.LabelFor(model => model.customer_id, new { @class = "control-label col-md-2" })

        <div class="col-md-10">

            @Html.EditorFor(model => model.customer_id)

            @Html.ValidationMessageFor(model => model.customer_id)

        </div>

    </div>

    <div class="form-group">

        @Html.LabelFor(model => model.product_fk, new { @class = "control-label col-md-2" })

        <div class="col-md-10">

            @Html.EditorFor(model => model.product_fk)

            @Html.ValidationMessageFor(model => model.product_fk)

        </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>

}

ProductController:

// GET: /Product/Create
public ActionResult Create()
{
     return View();
}

//
// POST: /Product/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "id,text,title,customer_id,product_fk")] t_review review)
{
        if (ModelState.IsValid)
        {
            prose.CreateReview(review);
            return RedirectToAction("Index");
        }
        return View(review);
}

When I use a simple view with actionlink it works but when I try to use partial View shows this msg

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Commerce.Domain.Entites.t_review]', but this dictionary requires a model item of type 'Commerce.Domain.Entites.t_review'.

Upvotes: 2

Views: 1806

Answers (1)

Mihai Dinculescu
Mihai Dinculescu

Reputation: 20033

Html.Partial renders a view without calling the Controller first.

Html.Action calls a Controller Action which can return like everything: a view, a partial view, json, etc.

As a general guideline, you should use Html.Partial only when you want to display static content or when you have access to the model required. For everything else, like wen you want to return more data from the server, use Html.Action.

Upvotes: 0

Related Questions