3

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'.

4 Answers 4

3

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

You've missed actual linq query execution with ToList() before passing model to thу view.
1

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);
}

Comments

1

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);
}

Comments

0

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()}
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.