简体   繁体   English

在asp.net mvc中列出并创建相同的视图

[英]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 然后我有List和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'. 传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [projectname.Models.Product]',但此字典需要类型为'projectname.Models.ProductViewModel'的模型项。

The error is self-explanatory, your view accepts @model albaraka.Models.ProductViewModel not @model IList<albaraka.Models.ProductViewModel> . 错误是不言自明的,您的视图接受@model albaraka.Models.ProductViewModel而不是@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 : 在您的视图中,您使用的是@model albaraka.Models.ProductViewModel ,它指示View所期望的模型是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. 您正在向视图提供Product对象列表,而不是它期望的ProductModelView。 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). 错误的直接原因是您作为参数(List)传递的内容与视图( ProductViewModel类的实例)所期望的内容类型不匹配。

In your controller, instead of the line: 在您的控制器中,而不是行:

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

Try the following: 请尝试以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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