-3

I have this variable in MVC .ASP NET Razor Synthax:

    var data = System.Web.HttpContext.Current.Request.Form["data"];

var data is a string. Is there any way i can type cast it into int?

thanks

Renato Fonseca
  • 25
  • 1
  • 1
  • 7

2 Answers2

3

You're looking for:

var intData = Convert.ToInt32(System.Web.HttpContext.Current.Request.Form["data"]);

But you definatly should read about MVC model binding. And name conversations and ViewModels and Strongly Typed Views.

When you understand how it work you won't need this convertions anymore.

teo van kot
  • 12,350
  • 10
  • 38
  • 70
2

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
     }
}
Lost_In_Library
  • 3,265
  • 6
  • 38
  • 70