I am trying to pass values entered by a user in a form to an ASP.NET MVC 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 try to submit the form, it does nothing. Please help me do this correctly to pass entries from the form to the controller.
This is my controller 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();
}
}
}
This is my index.cshtml view 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("GetProperties", "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>