简体   繁体   中英

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM