public IActionResult Post([FromBody] Restaurant restaurant) { restaurant = _restaurantData.Add(restaurant); _restaurantData.Commit(); return(Ok(restaurant)); }
[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 IActionResult OnPost() { IActionResult result = null; if (ModelState.IsValid) { if (this.Restaurant.Id <= 0) { this.Restaurant = restaurantData.Add(this.Restaurant); } else { this.Restaurant = restaurantData.Update(this.Restaurant); } this.restaurantData.Commit(); TempData["Message"] = "Restaurant saved!"; //Displays on the re-direct page. //When an update is done from a Post it is best to redirect off of the edit page to prevent //the user from refreshing the page which re-sends the update. result = RedirectToPage("./Detail", new { restaurantId = this.Restaurant.Id }); } else { result = Page(); } this.Cuisines = this.htmlHelper?.GetEnumSelectList <CuisineType>(); return(result); }
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 OnPost() { if (!ModelState.IsValid) { Cuisines = _htmlHelper.GetEnumSelectList <CuisineType>(); return(Page()); } // this relies on the db not using 0 as a record id value if (Restaurant.Id > 0) { _restaurantData.Update(Restaurant); } else { _restaurantData.Add(Restaurant); } _restaurantData.Commit(); // TempData is provided by ASPNet.Core to store temporary data. Gets reset on next page load TempData["Message"] = "Restaurant saved!"; // the anonymous object will pass the restaurant id through to the Details page // PRG pattern => POST Response GET i.e redirect to a GET request once success POST has been made return(RedirectToPage("./Detail", new { restaurantId = Restaurant.Id })); }
public IActionResult OnPost() { if (!ModelState.IsValid) { Cuisines = htmlHelper.GetEnumSelectList <CuisineType>(); return(Page()); //restaurantData.Update(Restaurant); //restaurantData.Commit(); //return RedirectToPage("./Detail", new { restaurantId = Restaurant.Id }); } // Restaurant=restaurantData.Update(Restaurant); // use for update // restaurantData.Update(Restaurant); // use for update // return update page if (Restaurant.Id > 0) { restaurantData.Update(Restaurant); } else { restaurantData.Add(Restaurant); } restaurantData.Commit(); TempData["Message"] = "Restaurant Saved!"; return(RedirectToPage("./Detail", new { restaurantId = Restaurant.Id })); }
[ValidateAntiForgeryToken] // ENSURES THAT WE HANDED OUT THIS FORM! // WE CREATE A EXTRA MODEL TO ONLY FILL IT WITH DATA WE REALLY NEED! // AND "CONVERT" IT TO OUR RESTAURANT MODEL public IActionResult Create(RestaurantEditModel model) { // TAGHELPERS WORK WITH MODELSTATE DATASTRUCT TO ASSOCIATE THE ERROR MESSAGES // https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-2.1 if (!ModelState.IsValid) { return(View()); } var newRestaurant = new Restaurant() { Name = model.Name, Cuisine = model.Cuisine }; newRestaurant = _restaurantData.Add(newRestaurant); // DANGEROUS: WE ARE STILL IN THE POST OPERATION, SO A REFRESH WOULD // SEND ANOTHER POST REQUEST TO OUR SERVER. USE REDIRECT TO FORCE THE CLIENT // TO SEND A GET REQUEST //return View("Details", newRestaurant); // WILL CONSULT ROUTING RULES TO IDENTIFY Id PARAMETER AS THE LAST SEGMENT OF THE URL // EARCH VALUE THAT IS NOT IN THE ROUTING RULE WILL BE PUT IN THE QUERY STRING (see foo) return(RedirectToAction(nameof(Details), new { Id = newRestaurant.Id, foo = "bar" })); }
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 IActionResult OnPost() { if (!ModelState.IsValid) { this.Cusines = htmlHelper.GetEnumSelectList <CusineType>(); return(Page()); } if (Restaurant.Id > 0) { TempData["Message"] = "Restaurant updated!"; Restaurant = restaurantData.Update(Restaurant); } else { TempData["Message"] = "New Restaurant saved!"; Restaurant = restaurantData.Add(Restaurant); } restaurantData.Commit(); return(RedirectToPage("./Details", new { restaurantId = Restaurant.Id } )); }
public ActionResult Create(Restaurant restaurante) { try { // TODO: Add insert logic here /* * UNA FORMA DE VALIDAR * if (String.IsNullOrEmpty(restaurante.Name)) * { * ModelState.AddModelError(nameof(restaurante.Name), "El nombre es requerido"); * } * * if (ModelState.IsValid) * { * db.Add(restaurante); * return RedirectToAction("Index"); * } * return View();*/ if (ModelState.IsValid) { db.Add(restaurante); //return RedirectToAction("Details", new { id = restaurante.Id }); TempData["Mensaje"] = "Creado correctamente"; return(RedirectToAction("Index")); } return(View()); } catch { return(View()); } }
public IActionResult OnPost() { Cuisines = _htmlHelper.GetEnumSelectList <CuisineType>(); if (!ModelState.IsValid) { return(Page()); } if (Restaurant.Id > 0) { Restaurant = _restaurantData.Update(Restaurant); } else { Restaurant = _restaurantData.Add(Restaurant); } if (Restaurant == null) { return(RedirectToPage("./NotFound")); } _restaurantData.Commit(); TempData["Message"] = "Restaurant saved!"; return(RedirectToPage("./Detail", new { restaurantId = Restaurant.Id })); }
public IActionResult OnPost() { //ModelState["Location"].Errors //ModelState["Location"].AttemptedValue //ModelState["Location"]. if (!ModelState.IsValid) { Cuisines = htmlHelper.GetEnumSelectList <CuisineType>(); return(Page()); } if (Restaurant.Id > 0) { Restaurant = restaurantData.Update(Restaurant); } else { restaurantData.Add(Restaurant); } restaurantData.Commit(); TempData["Message"] = "Restaurant saved!"; return(RedirectToPage("./Detail", new { restaurantId = Restaurant.Id })); }
public IActionResult OnPost() { if (!ModelState.IsValid) { Cuisines = _htmlHelper.GetEnumSelectList <CuisineType>(); //must add because .net is stateless return(Page()); } if (Restaurant.Id > 0) { Restaurant = _restaurantData.Update(Restaurant); } else { Restaurant = _restaurantData.Add(Restaurant); } _restaurantData.Commit(); //temp data goes away so no need to clean up. //On next request any page can get this message //...unless it's in cshtml TempData["Message"] = "Restaurant saved!"; return(RedirectToPage("./Detail", new { restaurantId = Restaurant.Id })); }
public IActionResult OnPost() { if (!ModelState.IsValid) { //update cuisines to show Cuisines = htmlHelper.GetEnumSelectList <CuisineType>(); // return the page back to the user so he can fix any errors return(Page()); } if (Restaurant.Id > 0) { Restaurant = restaurantData.Update(Restaurant); TempData["Message"] = "Restaurant updated!"; } else { restaurantData.Add(Restaurant); TempData["Message"] = "Restaurant created!"; } restaurantData.Commit(); //TempData["Message"] = "Restaurant saved!"; //return RedirectToPage("./List"); return(RedirectToPage("./Detail", new { restaurantId = Restaurant.Id })); }
public IActionResult OnPost() { //if (ModelState.IsValid) //{ // restaurantData.Update(Restaurant); // restaurantData.Commit(); // return RedirectToPage("./Details", new { restaurantId = Restaurant.ID }); //} ////just to keep/show the data inside the list or textbox in the UI, can be removed also //Cuisines = htmlHelper.GetEnumSelectList<CuisineType>(); //return Page(); ////return RedirectToPage("./List"); /// if (!ModelState.IsValid) { Cuisines = htmlHelper.GetEnumSelectList <CuisineType>(); return(Page()); } if (Restaurant.ID > 0) { restaurantData.Update(Restaurant); } else { restaurantData.Add(Restaurant); } restaurantData.Commit(); TempData["Message"] = "Restaurant Saved!"; return(RedirectToPage("./Details", new { restaurantId = Restaurant.ID })); }
public IActionResult OnPost() { //This is from all the valicators [Required] in Restaurant.cs file Errors and AttemptedValue //ModelState["Location"].Errors if (!ModelState.IsValid) { Cuisines = htmlHelper.GetEnumSelectList <CuisineType>();//asp core is stateless without this on post it will not populate return(Page()); } if (Restaurant.Id > 0) { restaurantData.Update(Restaurant); } else { restaurantData.Add(Restaurant); } restaurantData.Commit(); TempData["Message"] = "Restaurant saved!";//only available for the next request and then it will disappear. //you never want a resubmission return(RedirectToPage("./Detail", new { restaurantId = Restaurant.Id })); //pass the id to the Detail page which is needed after get to the page a get request is made //pattern post get redirect pattern }
public IActionResult OnPost() { //If invalid return if (!ModelState.IsValid) { Cuisines = htmlHelper.GetEnumSelectList <CuisineType>(); return(Page()); } //Valid record from this point on var msg = "Restaurnat saved!"; //Update logic (0 = new record) if (Restaurant.Id > 0) { Restaurant = restaurantData.Update(Restaurant); } else { restaurantData.Add(Restaurant); //Add logic msg = "Restaurnat added!"; } restaurantData.Commit(); TempData["Message"] = msg; return(RedirectToPage("./Detail", new { restaurantId = Restaurant.Id })); }
public IActionResult OnPost() { var message = ""; if (!ModelState.IsValid) { Cuisines = htmlHelper.GetEnumSelectList <CuisineType>(); return(Page()); } if (Restaurant.Id > 0) { Restaurant = restaurantData.Update(Restaurant); message = "updated"; } else { Restaurant = restaurantData.Add(Restaurant); message = "saved"; } restaurantData.Commit(); TempData["Message"] = "Restaurant @message!".Replace("@message", message); return(RedirectToPage("./Detail", new { restaurantId = Restaurant.Id })); }
// If you ommit the "public" modifier, there is no warning, // the page will still do something, but won't execute the // method; this can be very frustrating until you realize // what is going on. public IActionResult OnPost() { // ModelState has information about the request, for instance // the attempted value of a paramater that might be invalid, // and many; it has the information asp-for relies on // // But, most of the time, it is used whether all the parameters // in the request are valid. if (!ModelState.IsValid) { // The page model is stateless, so this property // need to be populated for every request. // // This is not an issue for the other properties, // because they were already populated, as input models, // by the request CuisineItems = htmlHelper.GetEnumSelectList <CuisineType>(); return(Page()); } // We assume that the database will never generate ids // that are 0, which is that of a new restaurant. if (Restaurant.Id == 0) { restaurantData.Add(Restaurant); } else { restaurantData.Update(Restaurant); } restaurantData.Commit(); // This is the right way of passing temporary data to // another page. // // The reason is that if it was passed as some kind of // parameter, and the user bookmarked that page, then // the now expired message would have been displayed // anyway. TempData["Message"] = "Restaurant Saved!"; // It is bad practice after making a POST request to // stay the same URL. The reason is that a POST request // typically creates new resources, which is not what you // may want to happen if you refresh the page. This is the // reason browser warn you before resubmitting such a page. // // The POST-GET-REDIRECT pattern: // // The conventional way around this is to redirect to a // "safe" page, one that would perform a GET request if // refreshed. // // C# allows you to create typeless objects, which is convenient // in this case to pass the route for the redirect. return(RedirectToPage("./Details", new { restaurantId = Restaurant.Id })); }
public ActionResult Create(Restaurant restaurant) { if (!ModelState.IsValid) { return(View(restaurant)); } restaurantData.Add(restaurant); return(RedirectToAction(nameof(Details), new { id = restaurant.Id })); }
public ActionResult Create(Restaurant restaurant) { if (ModelState.IsValid) { db.Add(restaurant); //adding a resta return(RedirectToAction("Details", new { id = restaurant.Id })); //create details view for an specific ID } return(View()); }
public ActionResult Create(Restaurant restaurant) { if (ModelState.IsValid) { db.Add(restaurant); return(RedirectToAction("Index")); } return(View()); }
public ActionResult Create(Restaurant restaurant) { if (ModelState.IsValid) { db.Add(restaurant); return(RedirectToAction("Details", new { id = restaurant.Id })); } return(View()); }
public IActionResult OnPost() { if (ModelState.IsValid) { RestaurantData.Add(Restaurant); TempData["Message"] = "New restaurant is saved!"; return(RedirectToPage("./List")); } return(Page()); }
public ActionResult Create(Restaurant restaurant) // MVC will try to populate this param // model binding { if (ModelState.IsValid) { db.Add(restaurant); return(RedirectToAction("Details", new { id = restaurant.Id })); } return(View()); }
public ActionResult Create(Restaurant restaurant) { if (ModelState.IsValid) { db.Add(restaurant); return(RedirectToAction("Details", new { id = restaurant.Id })); } return(View()); // represent with errors to allow user to fix }
public ActionResult Create(Restaurant restaurant) { if (ModelState.IsValid) { db.Add(restaurant); TempData["Message"] = "You have created the restaurant " + restaurant.Name + "!"; return(RedirectToAction("Details", new { id = restaurant.Id })); } return(View()); }
public void OnPost(Restaurant resCreateModel) { var resturarent = new Restaurant { Cuisine = resCreateModel.Cuisine, Name = resCreateModel.Name }; _restuarantData.Add(resturarent); Response.Redirect("all"); }
public IActionResult Create(RestaurantEditModel model) { var newRestaurant = new Restaurant(); newRestaurant.Name = model.Name; newRestaurant.Cuisine = model.Cuisine; newRestaurant = _restaurantData.Add(newRestaurant); return(RedirectToAction(nameof(Details), new { id = newRestaurant.Id })); }
public ActionResult Create(Restaurant restaurant) { if (!ModelState.IsValid) { return(View(restaurant)); } var created = _restaurantData.Add(restaurant); return(RedirectToAction("Details", new { id = created.Id })); }