c# - 在 asp.net mvc 的同一 View 中列出和创建

标签 c# .net asp.net-mvc linq asp.net-mvc-4

我有以下模型类

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

然后我对 List 和 Create 都有以下 View

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

这是列表的 Controller 方法

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

但是这里我遇到了以下错误

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

最佳答案

错误不言自明,您的观点接受@model albaraka.Models.ProductViewModel不是@model IList<albaraka.Models.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);
}

关于c# - 在 asp.net mvc 的同一 View 中列出和创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35059999/

相关文章:

c# - 无法将 System.Net.Utilities 与 DNX 4.5.1 一起使用

c# - 从 MVC 项目中删除 Entity Framework 引用

c# - 仅从共享点列表中获取自定义字段

c# - 将同步 zip 操作转换为异步

c# - 压缩音频编码

.net - 文件显示在 Visual Studio 的解决方案资源管理器中,并带有一个快捷方式图标。这意味着什么?

asp.net-mvc - Mono上的ASP.NET MVC Preview 5

c# - 使用c#比较两个文件夹内容

C# 十进制到数字(?,?)

.net - Visual Studio 2008 中缺少工作流项目模板