ASP Module 2
Getting Started with MVC
Getting Started with MVC
  • Overview
    • Introduction to MVC
  • Understanding MVC
    • Creating an MVC app
    • Add a controller
      • Understand HTTP endpoint
      • Understand basic routing
    • Add a view
      • Understanding shared layouts
      • Setting view title
      • Passing data to view
  • Exercise
    • Add two numbers
      • Using Model
  • Display Movies
    • Add a model
      • Scaffold the index view
      • Scaffold the movies controller
      • Add temp movie data
      • Pass data to index view
    • Implement actions
      • The first create action
      • The second create action
      • The details action method
      • Edit & Delete actions
      • Asynchronous actions
    • Repo link
Powered by GitBook
On this page
  • Preparing the second create action method
  • The current code in second create method
  • Modify the code as shown below
  • Let us further modify the code
  • We need to now implement the add method in our fake context as shown:
  1. Display Movies
  2. Implement actions

The second create action

Preparing the second create action method

Let us prepare the second create action method which will actually let the user create a new movie. But how is this called?

If you notice the last block of the Create.cshtml file, is has a button (input field):

<div class="form-group">
    <input type="submit" value="Create" class="btn btn-primary" />
</div>

This does the job of calling the second create method, which is an http POST call!

The current code in second create method

This is how the current code looks, which is a generic one emitted by the scaffolding. We need to modify it to be able to receive and process the movie info passed by the user via browser:

// POST: MoviesController/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(IFormCollection collection)
{
    try
    {
        return RedirectToAction(nameof(Index));
    }
    catch
    {
        return View();
    }
}

Modify the code as shown below

// POST: MoviesController/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Movie movie)
{
    if (ModelState.IsValid)
    {
        //todo: code to add movie
        return RedirectToAction(nameof(Index));
    }
    //send back what the user entered and don't present a blank form again!
    return View(movie);
}

Note the following in this code:

  • The method is accepting a movie object. So, we need the right name spaces for using this model.

  • ModelState is an object inherited from the controller base. We are checking if the passed object is valid.

  • If it is invalid, we need to send it back. So that the user will reenter the data.

  • If it is valid, we need to add the same to our collection. And then, Return back to the index view.

The movie object which the create action method is expecting is created by the framework by extracting the HTML form elements, this process is called binding.

Let us further modify the code

Let us save the movie in the fake context and also do some safety checks as shown:

// POST: MoviesController/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind("Id,Title,ReleaseDate,Genre,Price")] Movie movie)
{
    if (ModelState.IsValid)
    {
        _context.Add(movie);
        return RedirectToAction(nameof(Index));
    }
    //send back what the user entered and don't present a blank form again!
    return View(movie);
}

Note

  • The [HttpPost] attribute is required because we need to differentiate it from the get version of the create action

  • Also, we need to protect are endpoints from cross site forgery attacks. You can read more about it here:

We need to now implement the add method in our fake context as shown:

Implement add method in fake context

Add a method in the fake context class as shown below:

public void Add(Movie movie) => Movie.Add(movie);

The new movie information will not be persisted since we are not using a database. If we still want persist data (at least temporarily) without a database, we can make the required class, methods and movie collection static!

PreviousThe first create actionNextThe details action method

Last updated 2 years ago

To protect from overposting attacks, enable the specific properties you want to bind to. For more details, see

http://go.microsoft.com/fwlink/?LinkId=317598
Cross Site Request Forgery (CSRF) | OWASP Foundation
Logo