可能的重复:
是什么 ”??”操作员?

什么是 ?? 符号在这里表示?

我说的是正确的:使用 id, ,但是如果 id null使用字符串“ alfki”?

public ActionResult SelectionClientSide(string id)
        {
            ViewData["Customers"] = GetCustomers();
            ViewData["Orders"] = GetOrdersForCustomer(id ?? "ALFKI");
            ViewData["id"] = "ALFKI";
            return View();
        }
        [GridAction]
        public ActionResult _SelectionClientSide_Orders(string customerID)
        {
            customerID = customerID ?? "ALFKI";
            return View(new GridModel<Order>
            {
                Data = GetOrdersForCustomer(customerID)
            });
        }
有帮助吗?

解决方案

就是这样 零煤的操作员。

var x = y ?? z;

// is equivalent to:
var x = (y == null) ? z : y;

// also equivalent to:
if (y == null) 
{
    x = z;
}
else
{
    x = y;
}

IE: x 将被分配 z 如果 ynull, ,否则将分配 y.
因此,在您的示例中 customerID 将设置为 "ALFKI" 如果原来是 null.

其他提示

这是无效的合并操作员:http://msdn.microsoft.com/en-us/library/ms173224(vs.80).aspx

当第一个值(左侧)为空时,它提供了一个值(右侧)。

它的意思是“如果 id 或者 customerIDnull, ,假装是 "ALFKI" 反而。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top