如何在MVC .ASP NET中将变量从字符串强制转换为int [英] How to cast variable from string to int in MVC .ASP NET
本文介绍了如何在MVC .ASP NET中将变量从字符串强制转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在MVC .ASP NET Razor Synthax中具有此变量:
I have this variable in MVC .ASP NET Razor Synthax:
var data = System.Web.HttpContext.Current.Request.Form["data"];
var数据是一个字符串.我可以通过任何方式将其转换为int吗?
var data is a string. Is there any way i can type cast it into int?
谢谢
推荐答案
在剃须刀中,您可以使用@ {//c#code}语法.
In razor, you can use @{ //c# code } syntax.. Try this;
@{
int value;
if (int.TryParse(Request.Form["data"], out value))
{
// it's a number use the variable 'value'
} else {
// not a number
}
}
这篇关于如何在MVC .ASP NET中将变量从字符串强制转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文