在 asp.net MVC 的同一视图中列出和创建

本文关键字:创建 视图 net asp MVC | 更新日期: 2023-09-27 18:32:12

我有以下模型类

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

然后我对列表和创建都有以下视图

@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 
{}  

这是列表的控制器方法

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

但在这里我收到以下错误

传递到字典中的模型项的类型为 'System.Collections.Generic.List'1[projectname.型号.产品]',但是 此字典需要类型的模型项 '项目名称。模型.产品视图模型'.

在 asp.net MVC 的同一视图中列出和创建

错误是不言自明的,您的视图接受@model albaraka.Models.ProductViewModel而不是@model IList<albaraka.Models.ProductViewModel>。在您的视图中,您正在使用@model albaraka.Models.ProductViewModel,它指示视图预期的模型类型为 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);
}

该错误会准确地告诉您问题所在。您正在向视图而不是它所期望的产品模型视图提供产品对象列表。将控制器代码更改为以下内容:

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

好吧,异常文本是不言自明的 - 您将不正确的类型传递给视图。

它应该是

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

错误的直接原因是作为参数传递的内容(列表)与视图(ProductViewModel类的实例)所期望的内容的类型不匹配。

在控制器中,而不是行:

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

请尝试以下操作:

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