I'm trying create pagination on my page. The user can select the number of items that will appear per page, the preferred size then will be saved as cookie. But when I try to choose between the querystring parameter and cookie, an error occured:

    public ActionResult Index(string keyword, int? page, int? size)
    {
        keyword = keyword ?? "";
        page = page ?? 1;

        //Operator "??" cannot be applied to operands of type "int" and  "int"
        size = size ?? Convert.ToInt32(Request.Cookies.Get("StoriesPageSize").Value) ?? 10; 

What's causing this error? How to fix it?

有帮助吗?

解决方案

Convert.ToInt32 just returns int, not int? - so the type of the expression:

size ?? Convert.ToInt32(...)

is of type int. You can't use a non-nullable value type as the first operand of a null-coalescing operator expression - it can't possibly be null, so the second operand (10 in this case) could never possibly be used.

If you're trying to try to use the StoriesPageSize cookie, but you don't know whether or not it's present, you could use:

public ActionResult Index(string keyword, int? page, int? size)
{
    keyword = keyword ?? "";
    page = page ?? 1;

    size = size ?? GetSizeFromCookie() ?? 10;
}

private int? GetSizeFromCookie()
{
    string cookieValue = Request.Cookies.Get("StoriesPageSize").Value;
    if (cookieValue == null)
    {
        return null;
    }
    int size;
    if (int.TryParse(cookieValue, CultureInfo.InvariantCulture, out size))
    {
        return size;
    }
    // Couldn't parse...
    return null;
}

As mentioned in comments, you could write an extension method to make this more generally available:

public static int? GetInt32OrNull(this CookieCollection cookies, string name)
{
    if (cookies == null)
    {
        throw ArgumentNullException("cookies");
    }
    if (name == null)
    {
        throw ArgumentNullException("name");
    }
    string cookieValue = cookies.Get(name).Value;
    if (cookieValue == null)
    {
        return null;
    }
    int size;
    if (int.TryParse(cookieValue, CultureInfo.InvariantCulture, out size))
    {
        return size;
    }
    // Couldn't parse...
    return null;
}

Note that I've changed the code to use the invariant culture - it makes sense to propagate information in cookies in the invariant culture, as it's not really meant to be user-visible or culture-sensitive. You should make sure you save the cookie using the invariant culture too.

Anyway, with the extension method in place (in a static non-generic top-level class), you can use:

size = size ?? Request.Cookies.GetInt32OrNull("StoriesPageSize") ?? 10;

其他提示

The problem is that the result of the first operation (size ?? Convert.ToInt32(Request.Cookies.Get("StoriesPageSize").Value)) is an int. Then you use the Null Coalescing Operator between this int and another int, but since an int cannot be null, it fails.

It does not have a meaning to use the Null Coalescing Operator if the left hand side cannot be null, so the compiler gives an error.

Regarding how to fix it, you can rewrite it like this:

size = size ?? (Request.Cookies.Get("StoriesPageSize") != null ? Convert.ToInt32(Request.Cookies.Get("StoriesPageSize").Value) : 10);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top