kez
kez

Reputation: 2313

select multiple rows and edit those multiple rows in a table using linq asp.net mvc

I have table like below

enter image description here

Once I pass Product_ID as "01" I want to expand all relevant rows into editor view and once I submit those save those values into this table.

For GET method I need to select relevant rows and put those vaue intor edit boxes

So I create my get method like this

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

    }

but here I'm getting following exception message

The entity or complex type 'project_name.table_name' cannot be constructed in a LINQ to Entities query.

EDIT : this is the view page

Upvotes: 0

Views: 3255

Answers (2)

Ibrahim Khan
Ibrahim Khan

Reputation: 20740

You should select product_fields like following.

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

Since it return a list you view model should be

@model List<project_name.Models.AB_Product_vs_Field>

And in you view use a foreach loop to show all data like following.

@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 may be helpful for you. https://dotnetfiddle.net/XUJYAX

Upvotes: 2

Theunis
Theunis

Reputation: 338

Try the following

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

}

You are selecting all the fields so you don't have to create a anonymous object.

Upvotes: 0

Related Questions