I am making a page that will contains several dropdowns and a few textboxes. I have the data being stored in a viewmodel, that is then passed back to my [HttpPost] create method.
I am fairly new to viewmodels, and am wondering how to add a new database entry using data from a viewmodel.
When I go into Debug mode, all my viewmodel entities are populated, but MVC is not making the connection to make a new entry with those values. Current [Post] create method.
[HttpPost]
public ActionResult Create(OrdersViewModel vmobj)
{
if (ModelState.IsValid)
{
order obj = new order();
obj.projectNumber=vmobj.projectID.ToString();
obj.testingPhase = vmobj.testPhaseSt;
obj.unitSerial = vmobj.unitSerialSt;
obj.completeDate = vmobj.completeDate;
obj.closingStatement = vmobj.closingStatement;
db.orders.Add(obj);
return RedirectToAction("Create");
}
return View();
}
You need to call the DbContext.SaveChanges() to apply the changes in the database. Just like Jeroen told you.
Related
I need feature that is something similar to Laravel's old input helper but in MVC 5.
https://laravel.com/docs/5.6/requests#old-input
If validation fails, I need to reload all my model data as it was in the previous request except those inputs where user entered something wrong.
The problem is that my form has many disabled inputs and fields that program is fetching within [HttpGet] method, and they're getting lost during submission. So I need to store them in session.
The code below seems to work but is there any more efficient and beautiful way to do so with a less amount of code within each controller?
[HttpGet]
[Route(#"TaskManagement/Edit/{guid}")]
public async Task<ActionResult> Edit(Guid guid)
{
var model = new EditTaskViewModel();
model.Guid = guid;
await model.GetTaskFromRemoteService(new UserInfo(User));
ControllerHelpers.DisplayAlerts(model, this);
TempData["OldModel"] = model;
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
[Route(#"TaskManagement/Edit/{guid}")]
public async Task<ActionResult> Edit(EditTaskViewModel model, Guid guid, string submit)
{
model.Guid = guid;
if (ModelState.IsValid) {
await model.UpdateTaskInRemoteService(new UserInfo(User), submit);
ControllerHelpers.DisplayAlerts(model, this, "Task successfully updated");
if (model.ErrorCode == null)
return RedirectToAction("Edit", new { guid = model.Guid });
return RedirectToAction("Index");
}
if (TempData["OldModel"] != null) {
model = (EditTaskViewModel)TempData["OldModel"];
}
return View(model);
}
Using session state (including TempData) like this may break when you have multiple copies of the page open. You can work around this by generating a unique ID for the session key and storing it in a hidden field.
However, I would try to avoid using session altogether.
A simple approach is to use hidden fields to store the values that aren't sent to the server because they are in disabled fields.
A more robust approach is a separate class (or at least a private method) that knows how to setup your model for the first time and in transition (e.g. failed server validation). I call these classes "composers" and I describe the approach here.
Pseudocode for how an action method with a composer might look:
if( ModelState.IsValid ){
return Redirect();
}
var rebuiltModel = _composer.ComposeEdit( incomingModel );
return View( rebuiltModel );
I think the answer was quite simple. The shortest and easiest way is to populate the object from the database\remote service once more.
The fields that user entered whether they're valid or not will stay as they were before. The rest of them will load once again.
I’m confused on how to work with updating two entities while using a ViewModel. I’ve done some searching but I’m not coming up with anything that seems to help me understand this.
I have two entities Person and Address. I use a ViewModel to combine the two together in order to display them in my Edit.cshtml. At the top of my View I declare the ViewModel to be used.
Now, I get to the actual Edit portion. I’m assuming I would have to make an update to each entity and then create a new ViewModel to return to the View since the Veiw is expecting a ViewModel?
I’m also curious if there is a way to let the View know that the update was successful so I can display a message at the top of the View or if it would be better to just redirect to the Index View.
Is this a correct way of coding this or is there a more streamlined way to accomplish the same thing?
Top of my .cshtml page
#model Project.Models.MemberViewModel
Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult MyAccount(MemberViewModel model)
{
if (model.PersonId == 0)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
MemberViewModel updatedVM = new MemberViewModel();
using (var db = new DataContext())
{
Person currentPerson = db.Person.Find(model.PersonId);
if (currentPerson == null)
{
return HttpNotFound();
}
db.Entry(currentPerson).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
Address currentAddress = db.Address.Find(model.PersonId);
if (currentAddress == null)
{
return HttpNotFound();
}
db.Entry(currentAddress).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
////update and return a ViewModel
//updatedVM.FirstName = currentPerson.FirstName;
//updatedVM.LastName = currentPerson.LastName;
//updatedVM.Address = currentAddress.Address1;
//updatedVM.City = currentAddress.City;
//updatedVM.State = currentAddress.State;
//updatedVM.Zip = currentAddress.Zip;
}
return View(updatedVM);
}
In my opinion, the best practice would be to redirect to Index.cshtml after successfully saving the data (I think this is the whole idea of keeping MVC clean):
return RedirectToAction("Index");
I would only show an error message if there is trouble with the data.This can be done similar to this:
You can add these to the ViewModel:
bool hasErrors;
string errorMessage;
and in the view you would implement at the top of the page:
#if(Model.hasErrors)
{
<div>Model.errorMessage</div>
}
The idea is to validate the data inside the ViewModel constructor, or inside the controller, and if there is something wrong just set hasErrors = true and a custom errorMessage and show it in the view.cshtml.
Hope it helps.
I am working on a code project in Asp.net MVC. I have an issue with redirecting the user after they have completed an action. I have these controllers:
Index Search Page:
public ActionResult Index(){
//this method sets up viewmodel data for search preferences
Viewmodel obj = new Viewmodel();
//set values of dropdowns and searching capabilities
return View("Search", obj);
}
The user then fills out the search boxes in the view, chooses dropdowns. This will return a post search method that handles the data:
[HttpPost]
public ActionResult Index(Viewmodel obj, int? page)
{
data = from i in db.Database
select i;
if(!String.IsNullOrEmpty(obj.Example)
{
data = data.Where(x => x.poss == obj.poss);
}
//PAGING and other data formatting here
return View("Results", data);
}
Once the result list is displayed, I have a checkbox/button system in the result view that allows the user to select multiple results and mark them as "Good", "Bad" ETC. This is a method that changes the database very simply. My problem is that after the database alters the data, im not sure how to return the user back to the exact result set they were at. A method that returns void doesn't work, and the parameters are not separated, (one whole viewmodel), so i can't simply save the URL and return them back to the unique URL. I want to keep the viewmodel as the parameter. How can I save the viewmodel data that contains their search preferences for use later as well as the page number without changing my method to this:
[HttpPost]
public ActionResult Index(string dropdown1, string dropdown2, int num......){}
One thing I did was save view model to the session. Then I deserialize in the index method. Something like this in the index method:
[HttpGet]
public ActionResult AdvancedSearch()
{
HttpContext currentContext = System.Web.HttpContext.Current;
AdvancedSearchViewModel advancedSearchViewModel = (AdvancedSearchViewModel)Session["AdvancedSearchViewModel"];
if (advancedSearchViewModel == null)
{
advancedSearchViewModel = new AdvancedSearchViewModel();
AddAdvancedSearchLists(advancedSearchViewModel, currentContext);
}
return View(advancedSearchViewModel);
}
Here is some code to save to session in the post:
Session["AdvancedSearchViewModel"] = advancedSearchViewModel;
Note that if you have listboxes (drop down and multi select) you have to rebuild the listboxes in the post method and reselect the selections (for multi select). HTML is stateless. MVC does not send the listbox contents back to the server on the post, it only sends what was selected.
You can use TempData to achieve this.
Store ViewModel and Page in TempData within your POST Index action. The action method used to accept and store result status (i.e. good, bad, etc...) in the database will be able to access ViewModel and Page from the TempData given that it is the next immediate request. Once the database operation is done, just use RedirectToAction with the ViewModel and Page present in the TempData.
If the action which updates result status is not the next immediate request then you need to keep the data in session as answered by BGStack.
Preface:
I'm working on a project using MVC 5. We've built up our database from which our model is created. Then, we've added the Controllers and Views using Scaffolding. However in the Views we've omitted some of the properties (they should not be shown to the users).
Problem:
In the actions like Edit, as I hit the save button (to update the model) I encounter an exception and I think it requires me to provide the value of all the properties.
Please let me know how can I update just some of the properties (shown in the view)?
Please note that I need a solution as general as possible, so as to be able to use it in many Edit actions that I have in this project (Indeed this is the hard part).
Code:
The following are some of the codes that I think are most related to this question:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "AreaCode,Tels,Address")] Area area)
{//Area has many more properties (defined as required in the database) and I just need to update
//these : AreaCode,Tels,Address
if (ModelState.IsValid)
{
db.Entry(area).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Detector = new SelectList(db.Detectors, "DetectorCode", "detectorName", area.Detector);
ViewBag.Grade = new SelectList(db.Grades, "Gradeid", "GradeName", area.Grade);
return View(area);
}
SS:
Answers expressed in a simple way are highly appreciated.
Get your object to update from the database, and sets your model with the old value of properties
public ActionResult Edit([Bind(Include = "AreaCode,Tels,Address")] Area area)
{
YourDbContext _db= new YourDbContext();
Area oldArea = _db.Areas.Where(x => x.ID == area.ID).FirstOrDefault();
// Bind others properties marked with required from database
area.x = oldArea.x;
area.y = oldArea.y;
if (ModelState.IsValid)
{
db.Entry(area).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Detector = new SelectList(db.Detectors, "DetectorCode", "detectorName", area.Detector);
ViewBag.Grade = new SelectList(db.Grades, "Gradeid", "GradeName", area.Grade);
return View(area);
}
I'm trying to use ViewModels and AutoMapper, as I know these are best practice for avoiding lots of issues.
I'm ok using AutoMapper to populate a viewmodel, but I'm not as sure about how to update my database, from the ViewModel being posted back to my controller.
My GET is:
public ActionResult Edit(int id = 0)
{
Customer customer = db.Customers.Find(id);
var offers = db.Offers.Where(x => x.CustomerId == id).ToList();
var email = db.Emails.FirstOrDefault();
var vm = new CreateViewModel();
vm.CustomerId = customer.CustomerId;
vm.ArrivalDate = customer.ArrivalDate;
vm.CustomerName = customer.CustomerName;
vm.EmailAddress = customer.EmailAddress;
vm.NumNights = customer.NumNights;
vm.NumPeople = customer.NumPeople;
vm.EmailBody = email.EmailBody;
vm.From = email.From;
vm.Subject = email.Subject;
// Map list of Offers into ViewModel
vm.Offers = Mapper.Map<IList<Offer>, IList<OfferVM>>(offers);
return View(vm);
}
My POST is:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(EditViewModel vme)
{
if (ModelState.IsValid)
{
// Update the Customer properties
Customer customer = db.Customers.Find(vme.CustomerId);
customer.NumPeople = vme.NumPeople;
customer.NumNights = vme.NumNights;
customer.ArrivalDate = vme.ArrivalDate;
customer.CustomerName = vme.CustomerName;
customer.EmailAddress = vme.EmailAddress;
// Update Offers table
foreach (var o in vme.Offers)
{
// find the offer
Offer offer = db.Offers.Find(o.OfferId);
if (offer != null)
{
// update the properties of Offer
offer.RoomRate = o.RoomRate;
offer.IncludeInOffer = o.IncludeInOffer;
}
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(vme);
}
So my Post is manually updating two database tables (Offers and Customers).
The GET method is elegant, using AutoMapper, the POST is not. I'm wondering if there is a more straightforward way of updating the database via AutoMapper, without having to manually go through each property I am looking to update? Or is my POST controller as efficient as it can be?
Thank you for any pointers,
Mark
There is an overload of Map that allows to map properties to one preexisting instance:
Mapper.Map<IObjectA, IObjectB>(objectA, ObjectB);
Thus, you just have to map from the parameter vme (CreateViewModel vme) to the customer recovered from the DB. This also applies to the offers part.
Of course, you'll have to configure the mappings in the reverse direction: you've done it from db objects to view model, and now you need to map them from viewmodel to db objects. If you've followed Automapper conventions it will be pretty easy or even unnnecessary.
EDIT: added interesting comment by Henk Mollema
You can configure AutoMapper to ignore properties, if your ViewModel doesn't contain all the properties from your domain model (which is most likely the case), it won't overwrite them with nulls.
You first need to define your mapping like this:
Mapper.CreateMap<EditViewModel, Customer>();
Your updated action method could look like this. What happens here is your action method receives your view model and checks its validity. If it is valid then it does the mapping from your view model to your domain model. This domain model is then passed to your service or repository layer to update the cusotmer in the database. How you update the record depends entirely on you.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(EditViewModel viewModel)
{
if (!ModelState.IsValid)
{
return View(viewModel);
}
Customer customer = Mapper.Map<Customer>(viewModel);
customerService.Edit(customer);
return RedirectToAction("List");
}
I hope this can help guide you in the right direction. Your HttpGet action method will work in a similar way.