Hey guys am trying to pass values entered by a user in a form to an ASP.NET controller for processing to a database context. I do not know why the values in the form are not being passed to the controller method with the same parameter names, because when I tr to submit the form, it does nothing. Please help me do this correctly to pass entries from the form to the controller.
Below is mycontroller code

using Cars.Models;
using Microsoft.AspNetCore.Mvc;


namespace Cars.Controllers
{
    public class CarController:Controller
    {
        //declare the reference to database
        CarsContext _context ;
        public CarController()
        {
            _context = new CarsContext();
        }
        [HttpPost]
        //this method is supposed to receive the form values
        //and insert into the database
        public void GetProperties(string make,string model, string year,string nop)
        {
            //create a new car object
            Car car = new Car()
            {
                Make = make,
                Model = model,
                Year = Int32.Parse(year),
                NoPassengers = Int32.Parse(nop)

            };
            //create a new database context

            _context.Add(car);
            //save changes
            _context.SaveChanges();



        }
        //method to get all the cars
        public List<Car> GetAllCars()
        {

            //create a new database context
            return _context.Car.ToList();
        }
        //method to get a car by id
        public Car? GetById(int? id)
        {
            return _context.Car.FirstOrDefault(c => c.Id == id);
        }
       //method to check if a car exists
        public bool Exists(Car obj)
        {
            return _context.Car.Where(c => c.Id == obj.Id).Any();
        }
        //return cars whose passenger seats is three or less
        public List<Car> ThreePassengers()
        {
            return _context.Car.Where(c=>c.NoPassengers<=3).ToList();  
        }
    }
}

below is my index.cshtml form with the values I need passed to the controller via parameters

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <h1 class="display-4">Car Property Editor</h1>
   <p>Fill out the details of the car in the form below </p>
   <br/>
   <br/>
   @using (Html.BeginForm("ReceiveWithRequestFormData", "CarController"))
   {
   <table class="table">
     <thead>
         <tr>
             <td>Make</td>
             <td>Model</td>
             <td>Year Manufactured</td>
             <td>Number of Passengers</td>
         </tr>
     </thead>
     <tbody>
         <tr>
             <td>@Html.TextBox("make")</td>
               <td>@Html.TextBox("model")</td>
                 <td>@Html.TextBox("year")</td>
                   <td>@Html.TextBox("nop")</td>

         </tr>
     </tbody>
     <br/>

   </table>
    <center><input class="btn" type="submit" value="Insert"asp-action="myfunction()" style="background:blue;color:#111"></center>
   }
   <button class="btn">Get All Cars</button>
   <button class="btn">Get Car  by Id</button>
   <button class="btn">Get Car with three passenger seats</button>
   <button class="btn">Sort Cars by year manufactured</button>
</div>

Please help me hook the values in the form to the controller, Thank you.

I don't see an action called ReceiveWithRequestFormData, shouldn't that be GetProperties ?

Reopening the project in Visual Studio and try to see if your suggestions will work, will update you if everything works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.