【问题标题】:select multiple rows and edit those multiple rows in a table using linq asp.net mvc选择多行并使用 linq asp.net mvc 在表中编辑这些多行
【发布时间】:2016-03-09 04:40:48
【问题描述】:

我有如下表

一旦我将 Product_ID 作为“01”传递,我想将所有相关行展开到编辑器视图中,一旦我提交这些值,就将这些值保存到此表中。

对于 GET 方法,我需要选择相关行并将这些值放入编辑框

所以我创建了这样的 get 方法

    [HttpGet]
    public ActionResult Product_Edit(string Product_ID)
    {
        Product_ID = "01";

        var product_fields = (from productstatistics in db.AB_Product_vs_Field
                              where productstatistics.Product_ID == Product_ID
                              select new AB_Product_vs_Field
                             {
                                Product_ID = productstatistics.Product_ID,
                                Field_ID = productstatistics.Field_ID,
                                Field_Value_EN = productstatistics.Field_Value_EN,
                                Field_Value_AR = productstatistics.Field_Value_AR
                             }).ToList();

        if (product_fields == null)
        {
            return HttpNotFound();
        }


        return View(product_fields);

    }

但在这里我收到以下异常消息

无法在 LINQ to Entities 查询中构造实体或复杂类型“project_name.table_name”。

编辑:这是view page

【问题讨论】:

  • 能否提供您的查看页面代码或引发此错误的代码。
  • @JayeshGoyani 我更新了问题,LINQ 查询开始出现错误
  • 该视图是强类型视图,它的模型是“AB_Product_vs_Field”,您正在传递列表“List”代替视图中的对象“AB_Product_vs_Field”。

标签: c# asp.net-mvc entity-framework linq asp.net-mvc-4


【解决方案1】:

您应该选择product_fields,如下所示。

var product_fields = db.AB_Product_vs_Field.Where(p=>p.Product_ID==Product_ID);

由于它返回一个列表,您查看模型应该是

@model List<project_name.Models.AB_Product_vs_Field>

在您看来,使用foreach 循环来显示所有数据,如下所示。

@foreach (var item in Model)
{
    <div class="form-group">
        @Html.LabelFor(modelItem => item.Product_ID, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(modelItem => item.Product_ID, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(modelItem => item.Product_ID, "", new {@class = "text-danger"})
        </div>
    </div>
    @*others information goes here*@
}

This dotnet fiddle 可能对你有帮助。 https://dotnetfiddle.net/XUJYAX

【讨论】:

    【解决方案2】:

    试试下面的

    [HttpGet]
    public ActionResult Product_Edit(string Product_ID)
    {
        Product_ID = "01";
    
        var product_fields = (from productstatistics in db.AB_Product_vs_Field
                              where productstatistics.Product_ID == Product_ID
                              select productstatistics).ToList();
    
        if (product_fields == null)
        {
            return HttpNotFound();
        }
    
    
        return View(product_fields);
    
    }
    

    您正在选择所有字段,因此您不必创建匿名对象。

    【讨论】:

    • 然后收到此错误The model item passed into the dictionary is of type 'System.Collections.Generic.List1[project_name.Models.table_name]', but this dictionary requires a model item of type 'project_name.Models.table_name'
    猜你喜欢
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多