Search code examples
c#asp.net-mvcantiforgerytoken

ASP.NET antiforgerytoken saying that the object is null


I followed an exercise we did in class and I do not know why but @Html.AntiForgeryToken() is giving me the error

System.NullReferenceException: 'Object reference not set to an instance of an object.

<form method="post" action="\Files\Upload" enctype="multipart/form-data">
@Html.AntiForgeryToken()
    <input type="hidden" value="@Model.Id" name="id" />

    <label>Choose photo (,jpg\.png):</label>
    <input type="file" name="file" class="form-control" />
    <input type="submit" value="Upload Photo" />
</form>

Solution

  • Your forgot to return object in your action. Because your view is calling property @Model.Id

        public ActionResult Upload()
        {
            Test test = new Test();
            test.Id = 999;
    
            return View(test);
        }