Chathz
Chathz

Reputation: 743

List and create in same view in asp.net mvc

I have following model classes

public class ProductViewModel
{
    public Product Products { get; set; }
    public IEnumerable<Product> LProducts { get; set; }
}

public partial class Product
{    
    public string id { get; set; }
    public string detail { get; set; }
}

then I have following view for both List and Create

@model albaraka.Models.ProductViewModel    

<table class="table">
    <tr>

        <th>
            ID
        </th>
        <th>
            Detail
        </th>
    </tr>

    @foreach (var obj in Model.LProducts)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => obj.id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => obj.detail)
            </td>       
        </tr>   
    }

</table>

<div>
    @using ())
    {
        <div>
            <fieldset>
                <legend>Account Information</legend>

                <div class="editor-label">
                    @Html.LabelFor(m => m.id)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.Products.id)
                    @Html.ValidationMessageFor(m => m.Products.id)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(m => m.Products.detail)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.Products.detail)
                    @Html.ValidationMessageFor(m => m.Products.detail)
                </div>

                <p>
                    <input type="submit" value="Submit" />
                </p>
            </fieldset>
        </div>
    }
</div>

@section Scripts 
{}  

this is controller method for list

[HttpGet]
public ViewResult Product_List()
{
    ProductViewModel viewModelList = new ProductViewModel();

    viewModelList.LProducts = from products in db.Product
                  where products.details == "Pending"
                  select products;

    return View(viewModelList.LProducts.ToList());
}

but here I'm getting following error

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[projectname.Models.Product]', but this dictionary requires a model item of type 'projectname.Models.ProductViewModel'.

Upvotes: 3

Views: 2214

Answers (4)

rumata28
rumata28

Reputation: 395

The direct reason of the error is type mismatch of what you pass as argument (List) and what is expected by view (instance of ProductViewModel class).

In your controller, instead of the line:

return View(viewModelList.LProducts.ToList());

Try the following:

return View(
   new ProductViewModel {LProducts = viewModelList.LProducts.ToList()}
);

Upvotes: 0

Sirwan Afifi
Sirwan Afifi

Reputation: 10824

The error is self-explanatory, your view accepts @model albaraka.Models.ProductViewModel not @model IList<albaraka.Models.ProductViewModel>. In your view you are using @model albaraka.Models.ProductViewModel which indicates that the model expected by the View is of type ProductViewModel:

[HttpGet]
public ViewResult Product_List()
{
    ProductViewModel viewModelList = new ProductViewModel();

    viewModelList.LProducts = (from products in db.AB_Product
                  where products.details == "Pending"
                  select products).ToList();

    return View(viewModelList);
}

Upvotes: 3

Andrey Korneyev
Andrey Korneyev

Reputation: 26846

Well, exception text is self-explanatory - you're passing incorrect type to the view.

It should be

public ViewResult Product_List()
{
    ProductViewModel viewModelList = new ProductViewModel();

    viewModelList.LProducts = (from products in db.Product
                  where products.details == "Pending"
                  select products).ToList();

    return View(viewModelList);
}

Upvotes: 1

Paul Hunt
Paul Hunt

Reputation: 3555

The error tells you exactly what the issue is. You're supplying a list of Product objects to the view instead of the ProductModelView that it's expecting. Change your controller code to this:

[HttpGet]
public ViewResult Product_List()
{
    ProductViewModel viewModelList = new ProductViewModel();

    viewModelList.LProducts = (from products in db.Product
                  where products.details == "Pending"
                  select products).ToList();

    return View(viewModelList);
}

Upvotes: 1

Related Questions