Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
asp.net mvc 3带十进制类型的SelectList_.net_Asp.net Mvc 3 - Fatal编程技术网

asp.net mvc 3带十进制类型的SelectList

asp.net mvc 3带十进制类型的SelectList,.net,asp.net-mvc-3,.net,Asp.net Mvc 3,我试图在下拉列表中包含一个十进制值,但所选值无效。我把代码弄乱了一段时间,运气不好。最后,我使用了完全相同的代码,但将所有内容从十进制改为int,并且它可以工作 之前(InitialRewardPercent为十进制): @Html.DropDownListFor(x=>x.InitialRewardPercent, CommonServices.GetRewardTerminationInitialPercents(Model.InitialRewardPercent)) CommonServ

我试图在下拉列表中包含一个十进制值,但所选值无效。我把代码弄乱了一段时间,运气不好。最后,我使用了完全相同的代码,但将所有内容从十进制改为int,并且它可以工作

之前(InitialRewardPercent为十进制):

@Html.DropDownListFor(x=>x.InitialRewardPercent, CommonServices.GetRewardTerminationInitialPercents(Model.InitialRewardPercent))

CommonServices.GetRewardTerminationInitialPercents返回一个选择列表并选择我传入的值:

返回新的选择列表(奖励终止初始百分比,“百分比”, “百分比显示”,选择百分比)

之后(InitialRewardPercent为整数):

@Html.DropDownListFor(x=>x.InitialRewardPercent, CommonServices.GetRewardTerminationInitialPercents(Model.InitialRewardPercent *(100))

我做了*100因为我想从0.25升到25。基本上我所做的就是将变量类型从decimal切换到int,SelectList现在正确地将正确的行标记为selected


其他人能让SelectList使用十进制值吗?或者我做错了什么吗?

以下内容适合我:

型号:

public class MyViewModel
{
    public decimal InitialRewardPercent { get; set; }
    public IEnumerable<SelectListItem> Percents { get; set; }
}
视图:


正如预期的那样,当显示视图时,第二个项目将在下拉列表中预选。因此,我想在您的示例中,列表中没有与
初始报酬百分比值匹配的值,因此第一个值始终是预选的。

以下内容适用于我:

型号:

public class MyViewModel
{
    public decimal InitialRewardPercent { get; set; }
    public IEnumerable<SelectListItem> Percents { get; set; }
}
视图:

正如预期的那样,当显示视图时,第二个项目将在下拉列表中预选。因此,我想在您的示例中,列表中没有与
InitialRewardPercent
的值匹配的值,因此第一个值总是预选的。

我有一个类似的问题

我使用了
decimal.ToString()
来设置
SelectListItem.Value
so
(4.5m)。ToString()=“4.5”

但是,模型属性将添加一个额外的零,因为它在数据库中存储为4.50m,所以
(4.50m)。ToString()=“4.50”

在我的情况下,修复方法是使用格式化字符串-
decimal.ToString(“0.00”)
,因此它与model属性相同

public static IList<SelectListItem> ToSelectList(this IEnumerable<decimal> values, string format = "0.00")
{
            return values
                .Select(x => new SelectListItem() { Text = x.ToString(), Value = x.ToString(format) })
                .ToList();
}
publicstaticilist-ToSelectList(此IEnumerable值,字符串格式=“0.00”)
{
返回值
.Select(x=>new-SelectListItem(){Text=x.ToString(),Value=x.ToString(format)})
.ToList();
}
我也有类似的问题

我使用了
decimal.ToString()
来设置
SelectListItem.Value
so
(4.5m)。ToString()=“4.5”

但是,模型属性将添加一个额外的零,因为它在数据库中存储为4.50m,所以
(4.50m)。ToString()=“4.50”

在我的情况下,修复方法是使用格式化字符串-
decimal.ToString(“0.00”)
,因此它与model属性相同

public static IList<SelectListItem> ToSelectList(this IEnumerable<decimal> values, string format = "0.00")
{
            return values
                .Select(x => new SelectListItem() { Text = x.ToString(), Value = x.ToString(format) })
                .ToList();
}
publicstaticilist-ToSelectList(此IEnumerable值,字符串格式=“0.00”)
{
返回值
.Select(x=>new-SelectListItem(){Text=x.ToString(),Value=x.ToString(format)})
.ToList();
}

谢谢您的设置。当我这样做的时候,我在选择列表中的值也是小数,我看到你的是字符串。那一定是我做错了。谢谢。谢谢你的设置。当我这样做的时候,我在选择列表中的值也是小数,我看到你的是字符串。那一定是我做错了。谢谢