Related Tags:
Asp.Net It was developed by Microsoft to allow programmers to build dynamic web sites, web applications and web services. It was first released in January 2002 with version 1.0 of the .NET Framework, and is the successor to Microsoft's Active Server Pages (ASP) technology. Learn More, C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, ASP.NET MVC The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly. Learn More, Data Structure In computer science, a data structure is a particular way of organizing and storing data in a computer so that it can be accessed and modified efficiently. Learn More,

how to resolve "cannot convert from int? to int" in c#, compile time error?

Description:I have my controller method taking input parameter int? And one of my inner method taking int as input parameter so I basically wanted to know how to convert int?(Nullable int) to int in C#.

Posted by: | Posted on: Apr 29, 2022

1 answers

Replies

4

you can convert int? to int by using the Value property of Nullable int, you can consider following example let say your controller method taking nullable int as input parameter so you can use recordId as int in your inner method as shown.

public ActionResult YourControllerMethod(int? Id)
{
//following line will set 0 if the Id is null otherwise it will set the value into recordId
int recordId = Id == null ? 0 : Id.Value;
// Here you can simply use the recordId in your inner method as int
Records.GetbyId(recordId);
}

Replied by: | Replied on: Apr 29, 2022



Reply
×

Code block Hyperlink bold Quotes block Upload Images

Preview