Function giving the follwing Error : The parameters dictionary contains a null entry for parameter 'FileID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.PartialViewResult FileDetails(Int32) not seeing why>

App Dev 86 Reputation points
2022-04-26T22:28:11.913+00:00

Hi All,

The Function below gives this error, it is not clear why:

The parameters dictionary contains a null entry for parameter 'FileID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.PartialViewResult FileDetails(Int32)' in 'BFR_LOCAL_DB.Controllers.COController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

        <HttpGet>
        Public Function FileDetails(ByVal FileID As Integer) As PartialViewResult
            Dim DetList As List(Of FILE_ATTACHMENT_TBL) = GetFileList(FileID)
            Return PartialView("FileDetails", DetList)
        End Function

I try to call it two ways"

Way one: With not Parameter
http://localhost:60573/CO/FileDetails

When I try to pass a Parameter I get the same Error when I try call it this way
http://localhost:60573/CO/FileDetails/1945

But If I hard Code 1945 into the Function it works. See below
<HttpGet>
Public Function FileDetails() As PartialViewResult
Dim DetList As List(Of FILE_ATTACHMENT_TBL) = GetFileList(1945)
Return PartialView("FileDetails", DetList)
End Function

So oddly this works. What's throwing me is that the Error happens even before I am able to step into
the source codes to see why. It gives not errors during compile time.

There error is not very clear as it it saying there is an int32 but as it has been shown. The value is declared as integer.
so something has gone wacky over here I am guessing.

I am just try to the function before I wire it up to the rest of the program, Any help or ideas would be great.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,240 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 47,626 Reputation points
    2022-04-27T00:28:22.593+00:00

    The parameter name must be id. The default route automatically maps a path like {controller}/{action}/{id} to an action that accepts a parameter called id. Since your action doesn't have such a parameter it doesn't set it and therefore it cannot call the action. Just changing the parameter name will work.

    Alternatively you can also do this:

       http://localhost:60573/CO/FileDetails?fileID=1945  
    
    0 comments No comments

  2. SurferOnWww 1,906 Reputation points
    2022-04-27T01:40:02.153+00:00

    I guess that null is given to the argument FieldID of the action method FileDetails.

    Please try the followings:

    (1) Change the type of FieldID to nullable.

    (2) Validate if the FieldID is not null before processing. See the following sample code:

    public async Task<ActionResult> Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Products products = await db.Products.FindAsync(id);
        if (products == null)
        {
            return HttpNotFound();
        }
        return View(products);
    }
    
    0 comments No comments

  3. Yijing Sun-MSFT 7,061 Reputation points
    2022-04-27T02:25:18.81+00:00

    Hi @App Dev ,
    You could try to declare the "fileID" is a nullable type.

    Public Function FileDetails(ByVal FileID As Integer?) As PartialViewResult    //**use Integer?**  
        Dim DetList As List(Of FILE_ATTACHMENT_TBL) = GetFileList(FileID)  
        Return PartialView("FileDetails", DetList)  
    End Function  
    

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments