public ActionResult Create(Customer customer) { if (!ModelState.IsValid) { var viewModel = new CustomerFormViewModel() { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; return(View(nameof(CustomerForm), viewModel)); } _context.Customers.Add(customer); _context.SaveChanges(); return(RedirectToAction(nameof(Index))); }
public ActionResult Save(Customer customer) { //ModelState is used to get access to the data //IsValid is Used to change the application flow /* 3 steps to add server-side validation * 1. Add annotation in the model class * 2. Use ModelState and ModeState.IsValid and return View("Form", viewModel) in Controller Class * 3. Add Validation messages to the form * For Client-side validation go to CustomerForm.cshtml @section. */ if (!ModelState.IsValid) { var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; return(View("CustomerForm", viewModel)); } if (customer.Id == 0) { _context.Customers.Add(customer); } else { //Using auto Mapper //Mapper.Map(customer, customerInDb); //We don't use this because of security issues. //But in the case of updating only a specific values instead of all of them. //You can create a new class UpdateCustomerDto and use as the parameter here. //Dto stands for Data Transfer Object. var customerInDb = _context.Customers.Single(c => c.Id == customer.Id); customerInDb.Name = customer.Name; customerInDb.Birthdate = customer.Birthdate; customerInDb.MembershipTypeId = customer.MembershipTypeId; customerInDb.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter; } _context.SaveChanges(); return(RedirectToAction("Index", "Customers")); }
public ActionResult Edit(int id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return(Content("There's no customer with these credentials")); } var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; return(View("CustomerForm", viewModel)); }
public ActionResult Edit(int Id) { var customer = _unitOfWork.Customers.getDetail(Id); if (customer == null) { return(HttpNotFound()); } var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _unitOfWork.Customers.getMembershipTypes() }; return(View("CustomerForm", viewModel)); }
// private IEnumerable<Customer> GetCustomers() // { // return new List<Customer> // { // new Customer { Id = 1, Name = "John Smith" }, // new Customer { Id = 2, Name = "Mary Williams" } // }; // } public ActionResult Edit(int id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return(HttpNotFound()); } var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; //it search for the view with the name Edit, so use "" to overwrite it return(View("CustomerForm", viewModel)); }
public ActionResult Edit(int id) { var customer = _vidlyDbContext.Customers.SingleOrDefault(c => c.Id == id); customer.BirthDate.Value.ToLongDateString(); if (customer == null) { return(HttpNotFound()); } var viewmodel = new CustomerFormViewModel() { Customer = customer, MembershipTypes = _vidlyDbContext.MembershipTypes.ToList() }; return(View("CustomerForm", viewmodel)); }
// Action to create a new customer - with a support of viewModel (combination of mmshiptype + customer) with model binding to Save () class public ActionResult New() { // passing membershipTypes directly to the view can be done but it will not work later when we implement editing a customer. // Because editing require us to pass customer object to the field. // In this situation, we create a ViewModel that encapsulate the data required in the field var membershipTypes = _context.MembershipTypes.ToList(); // set the MembershipTypes viewmodel to the list of membership from membership table var viewModel = new CustomerFormViewModel { // Customer = new Customer() is created for validation summary to solve the id field is required problem as new customer will generate 0 as id Customer = new Customer(), MembershipTypes = membershipTypes }; return(View("CustomerForm", viewModel)); }
public IActionResult Edit(int id) { var customer = db.Customer.SingleOrDefault(c => c.Id == id); if (customer == null) { return(Content("not found")); } var ViewModel = new CustomerFormViewModel { Customer = customer, MembershipType = db.MembershipType.ToList() }; return(View("CustomerForm", ViewModel)); }
public ActionResult Edit(int Id) { var customer = _context.Customers.FirstOrDefault(c => c.CustomerID == Id); if (customer == null) { return(NotFound()); } var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; return(View("CustomerForm", viewModel)); }
public ActionResult Edit(int id) { using (var c = new ApplicationDbContext()) { var customer = c.Customers.SingleOrDefault(cu => cu.Id == id); if (customer == null) { return(HttpNotFound()); } var viewModel = new CustomerFormViewModel() { Customer = customer, MembershipTypes = c.MembershipTypes.ToList() }; return(View("CustomerForm", viewModel)); } }
public ActionResult Edit(int Id) { var customer = context.Customers.Find(Id); if (customer == null) { return(HttpNotFound()); } var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = context.MembershipTypes.ToList() }; return(View("Add", viewModel)); }
public ActionResult Edit(int id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return(HttpNotFound()); } var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; return(View("CustomerForm", viewModel)); }
public ActionResult Edit(int id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return(HttpNotFound()); } var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; //overwrites the standard behavior and goes to "CustomerForm" View instead return(View("CustomerForm", viewModel)); }
public ActionResult Edit(int id) { //Initialize customer var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return(HttpNotFound()); } var viewModel = new CustomerFormViewModel() { Customer = customer, //get the specific customer MembershipTypes = _context.MembershipTypes.ToList() //list the MembershipTypes for iteration in the dropdown lists }; return(View("CustomerForm", viewModel)); }
public IActionResult Edit(int id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return(NotFound()); } var model = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MembershipType.ToList() }; ViewBag.Title = "Edit Customer"; return(View("CustomerForm", model)); }
public ActionResult New() { //Get Membership Types from db so that you can use it in a dropdown in the view. var membershiptypes = _context.MembershipTypes.ToList(); var viewModel = new CustomerFormViewModel { MembershipTypes = membershiptypes }; //used in if statement at top of view to set the Heading of page, based on what we are doing, ie. New or edit. TempData["FormType"] = "New"; //below we ovveride the MVC convention to look for view with the same name //as the method. We specify the method.remember we do this because we are using the //same view and model for the new and edit methods and views. return(View("CustomerForm", viewModel)); }
public ActionResult Edit(int id) { var customer = this._context.Customers.SingleOrDefault(i => i.Id == id); if (customer == null) { return(HttpNotFound("No customer matching that id")); } else { var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = this._context.MembershipTypes.ToList() }; return(View("CustomerForm", viewModel)); } }
// Action to edit customer based on Id given through Html.ActionLink public ActionResult Edit(int id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return(HttpNotFound()); } // Change the class name by placing the cursor on its name, press F2 and all will change automatically var viewModel = new CustomerFormViewModel() { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; return(View("CustomerForm", viewModel)); }
public ActionResult Edit(int id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return(HttpNotFound()); } var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; // We can override the MVC convention, for convention the MVC search for the View with the same same of the Action name, but we can change it for another View result name as here) return(View("CustomerForm", viewModel)); }
public ActionResult Edit(int id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return(HttpNotFound()); } //We have to specify the view name here because it is different from this Action method's name. //This is also why we have to separately define the viewModel and pass it as a second argument. var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; return(View("CustomerForm", viewModel)); }
public ActionResult Edit(int id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return(HttpNotFound()); } var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; return(View("CustomerForm", viewModel)); // "New" to override MVC convention: to render <New> view, not <Edit> view }
//Edit Customer public ActionResult Edit(int Id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == Id); if (customer == null) { return(HttpNotFound()); } //The same view that we used for action New var viewmodel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; return(View("CustomerForm", viewmodel)); }
public ActionResult Edit(int id) //go to edit customer form { var customer = _context.Customers.FirstOrDefault(c => c.Id == id); if (customer == null) { return(HttpNotFound()); } var membershipTypes = _context.MembershipTypes.ToList(); var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = membershipTypes }; return(View("CustomerForm", viewModel)); }
public ActionResult Edit(int id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return(HttpNotFound()); } var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MemebershipTypes }; // Charger la vue 'New' , sinon il cherchera la vue 'Edit' qui n'existe pas return(View("CustomerForm", viewModel)); }
public ActionResult Details(int id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return(HttpNotFound()); } var viewModel = new CustomerFormViewModel { Customer = customer, }; return(View(customer)); }
public ActionResult Edit(int id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return(HttpNotFound()); } // view new vraca model: "NewCustomerViewModel" var viewmodel = new CustomerFormViewModel() { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; // prije smo vracali view new sada moramo overwriteati taj view return(View("CustomerForm", viewmodel)); }
public ActionResult Edit(int id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return(HttpNotFound()); } var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MemberShipTypes.ToList() }; //need specify viewname for returning New because it'll error trying to return Edit return(View("CustomerForm", viewModel)); }
public ActionResult Edit(int id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return(HttpNotFound()); } var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; // This will overwrite the view to display. Will look for a view named CustomerForm.cshtml, rather than Edit.cshtml return(View("CustomerForm", viewModel)); }
public ActionResult Edit(int id) { var customer = _context.Customers.SingleOrDefault(c => c.Id == id); if (customer == null) { return(HttpNotFound()); } var viewModel = new CustomerFormViewModel() { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; // Override View to point to 'New' view instead of default 'Edit' view. return(View("CustomerForm", viewModel)); }
public ActionResult Save(Customer customer) { if (!ModelState.IsValid) { var customViewMode = new CustomerFormViewModel() { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; return(View("CustomerForm", customViewMode)); } else { if (customer.Id == 0) { _context.Customers.Add(customer); } else { var customerInDb = _context.Customers.Single(c => c.Id == customer.Id); // Approach #1: // official microsoft suggest the following with property names whitelisted // but "magic strings": // TryUpdateModel(customerInDb, string.Empty, new string[] { "Name", "Email" }); //Approach #2: manually set each individual library customerInDb.Name = customer.Name; customerInDb.Birthdate = customer.Birthdate; customerInDb.MembershipTypeId = customer.MembershipTypeId; customerInDb.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter; //Approach #3: auto-mapper // Mapper.Map(customer, customerInDb); // pass a "data transfer object" to action which only includes properties that // needed to updated } _context.SaveChanges(); return(RedirectToAction("Index", "Customers")); } }