如何更新column = string的数据库

时间:2014-12-01 18:23:52

标签: database asp.net-mvc-4 updatemodel

我正在向控制器发送一个字符串,其中包含座位号:A1,A2等。以及包含ID的字符串。

我想从seat表中选择每个db行,其中column:seatnum等于seatss字符串中的每个座位号,并将名为status的相关列从0更新为ID字符串。

我使用下面的代码选择seatnum = seatss字符串的行,并将匹配的行作为json对象返回。如果字符串中有一个座位号,则有效,但如果有多个座位号则不行。我假设我需要实现某种类型的数组,但不知道该怎么做。座位编号如下:A1,A2,A3。所以我想在,上分离/爆炸它们,然后将它们与数据库行匹配。

    //
    // POST: /seats/



    public JsonResult Checkout(string seatss, string ID)
    {
        var seats = from b in db.seats
                    select b;


        seats = seats.Where(b => b.seatnum.Equals(seatss)); // Selects the db row where .seatnum = string seatss.

        //Update seatnum to the value of seatss


        return Json(seats, JsonRequestBehavior.AllowGet); // returns the selected seats as a json object.


    }

总而言之,我需要一种方法将字符串中的每个座位号与数据库中相应的seatnum相匹配,并将状态更新为ID字符串中包含的整数。

我的数据库表(席位)有两列名为seatnum和status。 seatnum列包含行:A1等,默认状态为0。

1 个答案:

答案 0 :(得分:0)

您可以更改传递座位号的方式,因此,不是名为seat的字符串参数,而是您的操作可能需要数组或字符串值的通用列表,或者您可以将当前字符串参数转换为数组然后修改你LINQ查询使用一个contains函数。提供的样本属于后一种实现,因为前者涉及的更多,当然这是未经测试的:

public JsonResult Checkout(string seatss, string ID)
{
    var seats = from b in db.seats
                select b;


    seats = seats.Where(b => seatss.Contains(b.seatnum));

    foreach(var seat in seats)
    {
         ... Perform status update on seat
    }

    return Json(seats, JsonRequestBehavior.AllowGet); // returns the selected seats as a json object.


}