Add/Insert data using repository MVC
Add/Insert data using repository MVC
I am currently being taught how to use MVC and my supervisor showed me how to use a repository with a read function but I'm stuck with implementing an Add function. This is my current code, thanks in advance!
Repository:
public void AddDriver(DriverModel model)
{
using (var db = new VehicleReservationEntities())
{
var newDriver = new Driver();
newDriver.DriverLastName = model.DriverLastName;
newDriver.DriverFirstName = model.DriverFirstName;
newDriver.DriverLicense = model.DriverLicense;
newDriver.LicenseExpiry = model.LicenseExpiry;
newDriver.MobileNumber = model.MobileNumber;
newDriver.BusinessUnit = model.BusinessUnit;
newDriver.DateRegistered = model.DateRegistered;
db.Driver.Add(newDriver);
db.SaveChanges();
}
}
Controller:
public ActionResult Add()
{
var repo = new VehicleRepository();
var data = repo.AddDriver();
var DVM = new DriverViewModel();
DVM.DriverLastName = data.DriverLastName;
DVM.DriverFirstName = data.DriverFirstName;
DVM.DriverLicense = data.DriverLicense;
DVM.LicenseExpiry = data.LicenseExpiry;
DVM.MobileNumber = data.MobileNumber;
DVM.BusinessUnit = data.BusinessUnit;
DVM.DateRegistered = data.DateRegistered;
db.Driver.Add();
db.SaveChanges();
}
In the controller, I get an error that I can't use void to assign to an implicit value. I'm not entirely sure what the next set of codes I should use.
– Joshua
Jun 29 at 17:06
In the controller you should call
AddDriver(DVM)
– Neel
Jun 29 at 17:18
AddDriver(DVM)
Your code seems a bit off.. the Repository's responsibility is to add a
DriverModel
object to the Driver
table in your database. Then in your controller, it seems you're trying to retrieve a Driver
object from the Repository.– M12 Bennett
Jun 29 at 17:35
DriverModel
Driver
Driver
1 Answer
1
There seems to be a few issues with what you've posted. I think you're looking for something like the following:
Controller:
[HttpPost]
public ActonResult Add(DriverViewModel DVM)
{
var repo = new VehicleRepository();
var driver = new Driver();
//tools such as automapper or tinymapper are often used so that you dont
//need to manually make these assignments
driver.DriverLastName = DVM.DriverLastName;
driver.DriverFirstName = DVM.DriverFirstName;
driver.DriverLicense = DVM.DriverLicense;
driver.LicenseExpiry = DVM.LicenseExpiry;
driver.MobileNumber = DVM.MobileNumber;
driver.BusinessUnit = DVM.BusinessUnit;
driver.DateRegistered = DVM.DateRegistered;
repo.AddDriver(driver);
//return whatever view you want to go to after the save
return View("Index");
}
Repository:
public void AddDriver(Driver newDriver)
{
using (var db = new VehicleReservationEntities())
{
db.Driver.Add(newDriver);
db.SaveChanges();
}
}
a few notes: normally, if you're using viewmodels then these viewmodels are posted to the controller instead of the raw EF entities. You were also calling db.SaveChanges
in the controller as well as the repository which doesn't seem like what you want. DbSet
s are also normally pluralized so your call to add the driver to the db would look like db.Drivers.Add(newDriver)
. You've also shown 3 different classes related to Drivers: Driver
, DriverModel
and DriverViewModel
. Sometimes this is necessary if your architecture has things separated out into multiple different layers for data access, business logic, and web however I don't see any evidence of that here. If everything exists in one project, I'd probably stick to DriverViewModel
and Driver
where Driver
is the EF entity and DriverViewModel
is what your views use and pass back to the controller
db.SaveChanges
DbSet
db.Drivers.Add(newDriver)
Driver
DriverModel
DriverViewModel
DriverViewModel
Driver
Driver
DriverViewModel
Hi! Thank you so much, this really helped! Though now I'm more conflicted now because my senior was apparently teaching me the wrong way. Anyways, does this work the same when inserting to multiple tables?
– Joshua
21 hours ago
@Joshua you can make your repository methods do anything you'd like . Generally, we strive to keep them as simple as possible and only have DB related work in them (any CRUD operations). And it's hard to say whether someone is teaching you the "wrong" way by the code shown. Also, if you're starting to do work on a large, pre-existing application then its better (imo) to stick with the scheme that already exists than it is to start implementing things in different ways.
– GregH
19 hours ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Where exactly are you stuck?
– Sahil Sharma
Jun 29 at 16:57