public IActionResult Create(RestaurantEditViewModel model) { // Make sure the Model is valid, if not then return back to the Create page if (ModelState.IsValid) { // Instantiate new restaurant that will hold the data from the EditViewModel var newRestaurant = new Restaurant(); // Assign the properties newRestaurant.Cuisine = model.Cuisine; newRestaurant.Name = model.Name; // Add the new restaurant to our list of restaurants. newRestaurant = _restaurantData.Add(newRestaurant); // Commit the changes to the database (SaveChanges to the DbContext) _restaurantData.Commit(); // Return the details of the new restaurant as a redirect return(RedirectToAction("Details", new { id = newRestaurant.Id })); } else { return(View()); } }
public IActionResult Create(RestaurantEditViewModel model) { if (ModelState.IsValid) { // create a new object of type Restaurant called newRestaurant var newRestaurant = new Restaurant(); // update the properties of the object newRestaurant newRestaurant.Cuisine = model.Cuisine; newRestaurant.Name = model.Name; newRestaurant = _restaurantData.Add(newRestaurant); // Post/Redirect/Get (or PRG) is a web development design pattern that helps to prevent // duplicate form submissions (See notes on Post-Redirect-Get). // the RedirectToAction(action, parameter) method causes the browser to // make a GET request to the specified action (i.e. "Details") with the specified id parameter. return(RedirectToAction("Details", new { id = newRestaurant.Id })); } // The Controller.View() method return an object of type ViewResult. // it can take an object (i.e. a model) and a 'ViewName' as parameters. // The returned ViewResult() object will render the specified view, if a ViewName is // specified as a parameter. However if the View() method is called with no parameters, // MVC will look for a View with the name of the calling action method. // Remember that all ASP.Net MVC Views must be contained in the folder (/Views) // MVC will by default look for the viewresult in the folders: - // {Views/controller_name/action_name.cshtml} i.e. /Views/Home/Create.cshtml // or {Views/Shared/action_name.cshtml} i.e. /Views/Shared/Create.cshtml return(View()); }
public async Task Create_ReturnsNewlyCreatedRestaurant() { _controller = new HomeController(_mapperMock.Object, _restaurantManagerMock.Object); JsonResultMessage message = new JsonResultMessage() { Message = "success", Success = true }; var newRestaurant = new Restaurant() { Name = "Lemongrass", Cuisine = CuisineType.Italian, }; var newRestaurantModel = new RestaurantEditViewModel() { Name = newRestaurant.Name, Cuisine = newRestaurant.Cuisine }; _restaurantManagerMock.Setup(repo => repo.AddNewRestaurantAsync(newRestaurant)) .Returns(Task.FromResult(message)); // Act var result = await _controller.Create(newRestaurantModel); // Assert var resultAction = Assert.IsType <RedirectToActionResult>(result); Assert.Equal("Details", resultAction.ActionName); Assert.Equal("Lemongrass", newRestaurant.Name); _restaurantManagerMock.Verify(); }
public IActionResult Edit(int?id) { if (id == null) { return(RedirectToAction("Error")); } string xmlFilePath = Path.GetFullPath("Data/resturant_reviews.xml"); restaurantReviews restaurantReviews = null; using (FileStream xs = new FileStream(xmlFilePath, FileMode.Open)) { XmlSerializer serializor = new XmlSerializer(typeof(restaurantReviews)); restaurantReviews = (restaurantReviews)serializor.Deserialize(xs); } if (id.Value < 0 || id.Value >= restaurantReviews.restaurant.Length) { return(RedirectToAction("Error")); } restaurantReviewsRestaurant restaurant = restaurantReviews.restaurant[id.Value]; RestaurantEditViewModel rest = RestaurantEditViewModel.GetRestaurantEditViewModel(restaurant); return(View(rest)); }
public IActionResult Edit(RestaurantEditViewModel rsVM) { string xmlFilePath = Path.GetFullPath("Data/resturant_reviews.xml"); try { restaurantReviews restaurantReviews = null; using (FileStream xs = new FileStream(xmlFilePath, FileMode.Open)) { XmlSerializer serializor = new XmlSerializer(typeof(restaurantReviews)); restaurantReviews = (restaurantReviews)serializor.Deserialize(xs); } restaurantReviewsRestaurant restaurant = restaurantReviews.restaurant[rsVM.Id]; restaurant.name = rsVM.Name; restaurant.address.street = rsVM.StreetAddress; restaurant.address.city = rsVM.City; restaurant.address.state_province = rsVM.ProvinceState; restaurant.address.postalCode = rsVM.PostalZipCode; restaurant.reviews.rivew.summary = rsVM.Summary; restaurant.reviews.rivew.rating.Value = (byte)rsVM.Rating; using (FileStream xs = new FileStream(xmlFilePath, FileMode.Create)) { XmlSerializer serializor = new XmlSerializer(typeof(restaurantReviews)); serializor.Serialize(xs, restaurantReviews); } } catch { return(RedirectToAction("Error")); } return(RedirectToAction("Index")); }
public async Task <IActionResult> Edit(RestaurantEditViewModel model) { if (ModelState.IsValid) { var restaurant = await repository.GetRestaurantAsync(model.Id); restaurant.Name = model.Name; restaurant.Phone = model.Phone; restaurant.Email = model.Email; restaurant.Description = model.Description; restaurant.Postcode = model.Postcode; restaurant.Unit = model.Unit; restaurant.Street = model.Street; restaurant.Town = model.Town; restaurant.StateCode = model.StateCode; restaurant.DateUpdated = DateTime.Now; repository.Update(restaurant); await unitOfWork.CompleteAsync(); return(RedirectToAction(nameof(Index))); } else { model.StateList = await repository.GetStatesAsync(); } return(View(model)); }
public IActionResult Edit(int?id) { restaurant_review restaurantList = null; string xmlFilePath = Path.GetFullPath("Data/restaurant_review .xml"); FileStream xs = new FileStream(xmlFilePath, FileMode.Open); XmlSerializer serializor = new XmlSerializer(typeof(restaurant_review)); restaurantList = (restaurant_review)serializor.Deserialize(xs); xs.Close(); //id = 0; int i = (int)id - 1; RestaurantEditViewModel restaurantEditView = new RestaurantEditViewModel() { Id = i, Name = restaurantList.restaurant[i].name, StreetAddress = restaurantList.restaurant[i].address.StreetAddress, City = restaurantList.restaurant[i].address.city, ProvinceState = restaurantList.restaurant[i].address.ProvinceState, PostalZipCode = restaurantList.restaurant[i].address.PostalZipCode, Rating = decimal.Parse(restaurantList.restaurant[i].reviews.review.rating.Value), Summary = restaurantList.restaurant[i].reviews.review.summary }; return(View(restaurantEditView)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var restaurant = await repository.GetRestaurantAsync(id.Value); if (restaurant == null) { return(NotFound()); } var restaurantEditViewModel = new RestaurantEditViewModel { Id = restaurant.Id, Name = restaurant.Name, Phone = restaurant.Phone, Email = restaurant.Email, Description = restaurant.Description, Postcode = restaurant.Postcode, Unit = restaurant.Unit, Street = restaurant.Street, Town = restaurant.Town, StateCode = restaurant.StateCode, StateList = await repository.GetStatesAsync() }; return(View(restaurantEditViewModel)); }
[ValidateAntiForgeryToken] //very important when authenticating users with cookies //also good to use when accepting posted form values public IActionResult Create(RestaurantEditViewModel model) { if (ModelState.IsValid) { var newRestaurant = new Restaurant(); newRestaurant.Cuisine = model.Cuisine; newRestaurant.Name = model.Name;//validation of view model will be executed at this tpoint newRestaurant = _restaurantData.Add(newRestaurant); //after adding restaurant, commit the data _restaurantData.Commit(); //redirect upon post of form to avoid duplicate recreation of data on refresh //pass second parameter for routing return(RedirectToAction("Details", new { id = newRestaurant.Id })); //View("Details", newRestaurant); } //redisplay view for user to fix the input return(View()); //if user forgot to input a field or inputted an invalid data //redisplay the view again with the data already inputted for a resave }
public async Task Edit_WhenCalled_UpdateCustomerAndRedirectToIndex() { // Arrange repository.Setup(r => r.GetRestaurantAsync(1)).ReturnsAsync(restaurant); var model = new RestaurantEditViewModel { Id = 1, Name = "Serving You", Phone = "02 1111 2222", Email = "*****@*****.**", Description = "Fantastic Restaurant", Postcode = "2000", Unit = "3", Street = "Market", Town = "Sydney", StateCode = "NSW" }; // Act var result = await controller.Edit(model) as RedirectToActionResult; // Assert Assert.That(restaurant.Street, Is.EqualTo("Market").IgnoreCase); repository.Verify(r => r.Update(restaurant), Times.Once); unitOfWork.Verify(u => u.CompleteAsync(), Times.Once); Assert.That(result.ActionName, Is.EqualTo("Index").IgnoreCase); }
public async Task EditAsyncEditsRestaurantWhenImageStaysTheSame() { await this.AddTestingDestinationToDb(); await this.AddTestingRestaurantToDb(); var newAddress = "New address"; var newSeats = 48; Assert.NotEqual(newAddress, this.DbContext.Restaurants.Find(TestRestaurantId).Address); Assert.NotEqual(newSeats, this.DbContext.Restaurants.Find(TestRestaurantId).Seats); var restaurantEditViewModel = new RestaurantEditViewModel() { Id = TestRestaurantId, Name = TestRestaurantName, DestinationId = TestDestinationId, Type = TestRestaurantType, Address = newAddress, Seats = newSeats, NewImage = null, }; await this.RestaurantsServiceMock.EditAsync(restaurantEditViewModel); Assert.Equal(newAddress, this.DbContext.Restaurants.Find(TestRestaurantId).Address); Assert.Equal(newSeats, this.DbContext.Restaurants.Find(TestRestaurantId).Seats); }
public IActionResult Edit(int id, RestaurantEditViewModel model) { var restaurant = _restaurantData.Get(id); if (ModelState.IsValid) { restaurant.Cuisine = model.Cuisine; restaurant.Name = model.Name; return(RedirectToAction("Details", new { id = restaurant.Id })); } return(View(restaurant)); }
public IActionResult Create(RestaurantEditViewModel model) { var newRestaurant = new Restaurant(); newRestaurant.Name = model.Name; newRestaurant.Cuisine = model.Cuisine; newRestaurant = _restaurantData.Add(newRestaurant); _restaurantData.Commit(); return(RedirectToAction("Details", new { id = newRestaurant.Id })); }
public IActionResult Edit(int RestaurantId, RestaurantEditViewModel restaurant) { if (!ModelState.IsValid) { restaurant.ResetCategoryList(context); return(View(restaurant)); //return View(new RestaurantEditViewModel()); } restaurant.Persist(RestaurantId, context); return(RedirectToAction(actionName: nameof(Index))); }
public IActionResult Create(RestaurantEditViewModel model) { var newRestaurant = new Restaurant { Cuisine = model.Cuisine, Name = model.Name }; newRestaurant = _restaurantsData.Add(newRestaurant); return(View("Details", newRestaurant)); }
public IActionResult Create(RestaurantEditViewModel model) { if (ModelState.IsValid) // related with data annotations, eg [Required] { var newRestaurant = new Restaurant(); newRestaurant.Cuisine = model.Cuisine; newRestaurant.Name = model.Name; newRestaurant = _restaurantData.Add(newRestaurant); _restaurantData.Commit(); return(RedirectToAction("Details", new { id = newRestaurant.Id }));//redirect to avoid multiple post request after refresh } return(View()); }
public IActionResult Edit(int id, RestaurantEditViewModel input) { var restaurant = _restaurantData.Get(id); if (restaurant != null && ModelState.IsValid) { restaurant.Name = input.Name; restaurant.Cuisine = input.Cuisine; _restaurantData.Commit(); return(RedirectToAction("Details", new { id = restaurant.Id })); } return(View(restaurant)); }
public IActionResult Create(RestaurantEditViewModel restaurant) { if (ModelState.IsValid) { var rest = new Restaurant(); rest.name = restaurant.Name; rest.CusineType = restaurant.CusineType; _restaurantData.Add(rest); return(RedirectToAction("Details", new { id = rest.Id })); } return(View(restaurant)); }
public IActionResult Create(RestaurantEditViewModel model) { if (ModelState.IsValid) { var restuarant = new Restaurant(); restuarant.Name = model.Name; restuarant.Cuisine = model.Cuisine; _restaurantData.Add(restuarant); _restaurantData.Commit(); return(RedirectToAction("Details", new { id = restuarant.Id })); } return(View()); }
public async Task <IActionResult> Edit(int Id, RestaurantEditViewModel input) { var restaurant = await _restaurantManager.GetRestaurantAsync(Id); if (restaurant != null && ModelState.IsValid) { restaurant.Name = input.Name; restaurant.Cuisine = input.Cuisine; await _restaurantManager.UpdateAsync(restaurant); return(RedirectToAction("Details", new { id = restaurant.Id })); } return(View(restaurant)); }
public IActionResult Create(RestaurantEditViewModel model) { var newRestaurant = new Restaurant(); newRestaurant.Cuisine = model.Cuisine; newRestaurant.Name = model.Name; _restaurantData.Add(newRestaurant); //return View("Details", newRestaurant); //duplicate post will happen //return RedirectToAction("Details", newRestaurant); return(RedirectToAction("Details", new { id = newRestaurant.Id })); }
public ActionResult Edit(RestaurantEditViewModel input) { var config = new MapperConfiguration(cfg => cfg.CreateMap <RestaurantEditViewModel, Restaurant>()); var mapper = config.CreateMapper(); //Copy values Restaurant restaurantToUpdate = mapper.Map <Restaurant>(input); restaurantToUpdate.RestaurantId = Convert.ToInt32(TempData["RestaurantId"]); restaurantToUpdate.UserId = Convert.ToInt32(Session["UserId"]); _restaurantContext.Update(restaurantToUpdate); return(RedirectToAction("Details", new { id = restaurantToUpdate.RestaurantId })); }
public IActionResult Edit(int id, RestaurantEditViewModel restaurant) { if (ModelState.IsValid) { var rest = _restaurantData.Get(id); rest.name = restaurant.Name; rest.CusineType = restaurant.CusineType; _restaurantData.Update(rest); return(RedirectToAction("Details", new { id = rest.Id })); } return(View(restaurant)); }
public IActionResult Create(RestaurantEditViewModel model) { if (ModelState.IsValid) { var newRestaruant = new Restaurant { Name = model.Name, Cuisine = model.Cuisine }; newRestaruant = _restuarantData.Add(newRestaruant); return(RedirectToAction(nameof(Details), new { id = newRestaruant.Id })); } return(View()); }
//[ValidateAntiForgeryToken] public IActionResult Edit(int id, RestaurantEditViewModel model) { var restaurant = _restaurantData.Get(id); if (ModelState.IsValid) { restaurant.Cuisine = model.Cuisine; restaurant.Name = model.Name; _restaurantData.Commit(); return(RedirectToAction("Details", new { id = restaurant.Id })); } // if the model state is invalid, give users the chance to fix the data return(View(restaurant)); }
public async Task <IActionResult> Edit(RestaurantEditViewModel restaurantEditView) { if (restaurantEditView.NewImage != null) { var fileType = restaurantEditView.NewImage.ContentType.Split('/')[1]; if (!this.IsImageTypeValid(fileType)) { return(this.View(restaurantEditView)); } } await this.restaurantsService.EditAsync(restaurantEditView); return(this.RedirectToAction("Details", "Restaurants", new { area = "", id = restaurantEditView.Id })); }
public IActionResult Create(RestaurantEditViewModel restaurant) { if (ModelState.IsValid) { var newRestaurante = new Restaurant() { Name = restaurant.Name, Cuisine = restaurant.Cuicine }; newRestaurante = _restaurantData.Add(newRestaurante); _restaurantData.Commit(); return(RedirectToAction("Detail", new { newRestaurante.Id })); } return(View()); }
public IActionResult Create(RestaurantEditViewModel model) { if (ModelState.IsValid) { var restaurant = new Restaurant { Name = model.Name, Cuisine = model.Cuisine }; _restaurantData.Add(restaurant); // return View("Details", restaurant); return(RedirectToAction("Details", new { id = restaurant.Id })); // TODO :fix redirection to details } return(View()); }
public IActionResult Create(RestaurantEditViewModel model) { if (ModelState.IsValid) { var newRestaurant = new Restaurant(); newRestaurant.Name = model.Name; newRestaurant.Cuisine = model.Cuisine; newRestaurant = _restaurantData.Add(newRestaurant); // return View("Details", newRestaurant); return(RedirectToAction("Details", new { id = newRestaurant.Id })); } else { return(View()); } }
public async Task EditAsyncThrowsArgumentExceptionIfRestaurantTypeInvalid() { await this.AddTestingDestinationToDb(); var invalidRestaurantEditInputModel = new RestaurantEditViewModel() { Name = TestRestaurantName, DestinationId = TestDestinationId, Type = InvalidRestaurantType, }; var exception = await Assert.ThrowsAsync <ArgumentException>(() => this.RestaurantsServiceMock.EditAsync(invalidRestaurantEditInputModel)); Assert.Equal(string.Format(ServicesDataConstants.InvalidRestaurantType, invalidRestaurantEditInputModel.Type), exception.Message); }