gpt4 book ai didi

c# - 如何在 mvc4 c# 中递减 nullable int by1?

转载 作者:行者123 更新时间:2023-11-30 14:25:13 25 4
gpt4 key购买 nike

我有如下一种操作方法:

public PartialViewResult batchSave(int? clientId, string uploadidList, int? currentFilter, int? page)
{
if(page!=null)
{
page--;// This is also not working
page=page-1; //Does not works
}
}

我按上面的方法试过了,但它没有减少。基本上它是可以为空的;那么有什么方法可以解决这个问题吗?谢谢

最佳答案

使用 -- 的简单递减工作正常。

int? t = 50;
t--; // t now == 49

我猜,问题出在比较此方法后的结果:

public void Dec(int? t) 
{
if (t != null)
{
t--; //if initial t == 50, then after this t == 49.
}
}
...
int? t = 50;
Dec(t); // but here t is still == 50

看看@PaulF 的回答,它包含解释,为什么 int? 的副本传递给方法,而不是引用。

因为您不能使用 refout 关键字标记 ASP.NET MVC4 Controller 方法的参数(它会在调用时导致 ArgumentException方法),我建议您使用具有多个属性的单个类。

因此,在递减时,您将处理通过引用传递的类属性,而不是 int? 变量的副本(AFAIK 这是一个很好的做法)。

在您的情况下,您的代码可以修改如下:

public class PassItem 
{
public int? clientId { get; set; }
public string uploadidList { get; set; }
public int? currentFilter { get; set; }
public int? page { get; set; }
}

public PartialViewResult batchSave(PassItem passItem)
{
if(passItem.page != null)
{
passItem.page--;
}
}

在这种情况下,您将使用一个对象,而不是对象的多个副本。

如果您从 View 调用方法,ASP.NET 默认绑定(bind)器将自动创建 PassItem 的实例,并使用所需的值设置它的属性。

关于c# - 如何在 mvc4 c# 中递减 nullable int by1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39287843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com