0

pass an id to an action method in asp.net mvc,id’s data returned successfully but also id=0 pass to action method and I got NullReferenceException pass an id to an action method in asp.net mvc,id’s data returned successfully but also id=0 pass to action method and I got NullReferenceException

public IActionResult ProjectDetails(long id)
        {
            var projectById = Data.ProjectStore.GetProjectsBy(id);
            return View(projectById);
        }
<section class="contact-section px-3 py-5 p-md-5">
    <div class="container">
        <table class="table table-bordered table-hover">
        <thead>
            

                <tr>
                    <td>ID</td>
                    <td>Name</td>
                    <td>Description</td>
                    <td>Client</td>
                    <td>Image</td>
                </tr>

        </thead>
        <tbody>
            <tr>
                <td>@Model.Id</td>
                <td>@Model.Name</td>
                <td>@Model.Description</td>
                <td>@Model.Client</td>
                <td><img src="~/assets/images/project/@Model.Image" width="50px" /></td>
            </tr>
        </tbody>
        </table>
</div>

</section>
public class ProjectStore
{
    public static List<Models.Project> GetProjects()
    {
         return new List<Models.Project>
        {
            new Models.Project(1,"PetService","Take care about pets ...","pet.jpeg","MehPet"),
            new Models.Project(2,"BabyService","Take care about babies ...","baby.jpeg","MehBabe"),
            new Models.Project(3,"FlowerService","Take care about flowers ...","flower.jpeg","MehFlow"),
            new Models.Project(4,"PersonalService","Take care about personal ...","personal.jpeg","MehPer"),

        };
    }

    public static Models.Project GetProjectsBy(long id) => GetProjects().FirstOrDefault(x => x.Id == id);
}
MoHoMe
  • 1
  • 3
  • Without looking in to it... I would guess that passing 0 to `Data.ProjectStore.GetProjectsBy(id);` would not find anything and return null. – phuzi May 25 '23 at 08:12
  • Because when you call _GetProjectsBy(id);_ the method cannot find the project but the code still returns a variable as model that is not set to anything (is null) You cannot use a variable with a null value to reference some property. But please read the duplicate because it is very informative – Steve May 25 '23 at 08:13
  • @phuzi no ,correct id passed to it and return data successfully ,the problem is after that, id=0 passed to action method also and really no data exists for it to return – MoHoMe May 25 '23 at 09:02
  • @Steve Thanks Steve,as I said before,at first it finds project and returns data in view correctly but I don't know why an id=0 pass again to method after that . – MoHoMe May 25 '23 at 09:28
  • Difficult to say then. I suggest you to look at the call stack when the second call is received by the controller (or page) to understand who is calling again a second time this method (I suppose it is a GET right?) In the code above there is nothing that would cause such second call. Anyway a check on the returned value from GetProjectsBy will be mandatory because you can't trust the input coming from the request pipeline (is always originated by the browser in the hand of your users) – Steve May 25 '23 at 14:18
  • @Steve I checked call stack out and found <.Model.get returned null>> in output. – MoHoMe May 26 '23 at 19:40
  • I used ModelState.IsValid in ProjectDetails action method and now it is ok. – MoHoMe May 29 '23 at 10:18

0 Answers0