简体   繁体   English

使用Request.QueryString数组更新ASP.NET中的排序列

[英]Updating sort column in ASP.NET with Request.QueryString Array

I'm passing a list of guids in a GET request from a JQuery Ajax call. 我正在从JQuery Ajax调用的GET请求中传递Guid列表。

on my ASP.NET controller side I want to iterate through the list and update the Display_Sort column to match my newly sorted list. 在我的ASP.NET控制器端,我想遍历列表并更新Display_Sort列以匹配新排序的列表。

My ID is a Guid and I'm getting a type error in the following code, because it's a string that I'm passing to the Db. 我的ID是Guid,在以下代码中出现类型错误,因为这是我要传递给Db的字符串。 However, I can't seem to convert the item(string) into a Guid. 但是,我似乎无法将item(string)转换为Guid。

I've tried Guid(item) and it would allow the constructor. 我试过Guid(item),它将允许构造函数。 Not sure what I'm missing. 不知道我在想什么。

Here is the code: 这是代码:

        //REORDER HOME ASSETS
    public ActionResult ReOrderHome()
    {
        using (var db = new IFEntities())
        {
            var myString = Request.QueryString;
            var i = 1;

            foreach (var item in myString)
            {
               var myObj = db.HomeContents.Find(item);
                myObj.display_order = i;
                db.SaveChanges();
                i++;
            }
        } 

You can convert item to GUID and then compare like this. 您可以将item转换为GUID ,然后像这样进行比较。

var myObj = db.HomeContents.Find(new Guid(item));

Or, you can use select instead of find . 或者,您可以使用select代替find Syntax for select -- select的语法-

foreach (var item in myString)
{
    var myObj = db.HomeContents.Select(p => p.<GUID_COLUMN_NAME> == item);
    myObj.display_order = i;
    db.SaveChanges();
    i++;
}

Replace GUID_COLUMN_NAME with actual column name. 用实际的列名替换GUID_COLUMN_NAME

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM