6 - examining the edit methods and edit view _ official microsoft site

Upload: correia97

Post on 04-Feb-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/21/2019 6 - Examining the Edit Methods and Edit View _ Official Microsoft Site

    1/13

    Home/MVC/Tutorials /Chapter 8. ASP.NET MVC 4 Beta/Getting Started with ASP.NET MVC 4/ Examining the

    Edit Methods and Edit View

    In this section, you'll examine the generated action methods and views for the movie controller. Then you'll add a

    custom search page.

    Run the application and browse to theMoviescontroller by appending/Moviesto the URL in the address bar of your

    browser. Hold the mouse pointer over an Editlink to see the URL that it links to.

    The Editlink was generated by the Html.ActionLinkmethod in the Views\Movies\Index.cshtmlview:

    @Html.ActionLink("Edit","Edit",new{id=item.ID})

    The Htmlobject is a helper that's exposed using a property on theWebViewPagebase class. The ActionLink

    method of the helper makes it easy to dynamically generate HTML hyperlinks that link to action methods on

    controllers. The first argument to the ActionLinkmethod is the link text to render (for example,EditMe).

    Try MVCcontrols that

    will truly save

    you time!

    www.telerik.com/ajax

    THIS IS A MULTI-PART ARTICLE

    Now viewing article 6 of 9

    Intro to ASP.NET MVC 4

    Adding a Controller

    Adding a View

    Adding a Model

    Accessing Your Model's Data

    from a Controller

    Examining the Edit Methods

    and Edit View

    Adding a New Field to the

    Movie Model and Table

    Adding Validation to the

    Model

    Examining the Details and

    Delete Methods

    Overview Videos Samples Forum Books Open Source

    By Rick Anderson | February 15, 2012

    Examining the Edit Methods and Edit View

    Home Get Started Downloads Web Pages Web Forms MVC Community ForumsSolutions

    Search ASP.NET Sign In | Join

    Privacy Statement | Terms of Service | Site Feedback | Advertise With Us

    Powered by and Umbraco

    Twitter | Facebook

    Follow Us On:

    Feedback on ASP.NET | File Bugs

    mining the Edit Methods and Edit View : Official Microsoft Site http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-m...

    13 20/04/2012 19:41

  • 7/21/2019 6 - Examining the Edit Methods and Edit View _ Official Microsoft Site

    2/13

    The generated link shown in the previous image is http://localhost:xxxxx/Movies/Edit/4. The default route (established

    in Global.asax.cs) takes the URL pattern {controller}/{action}/{id}. Therefore, ASP.NET translates

    http://localhost:xxxxx/Movies/Edit/4into a request to the Editaction method of the Moviescontroller with the

    parameter IDequal to 4.

    You can also pass action method parameters using a query string. For example, the URLhttp://localhost:xxxxx/Movies

    Edit?ID=4also passes the parameter IDof 4 to the Editaction method of the Moviescontroller.

    Open the Moviescontroller. The two Editaction methods are shown below.

    //

    //GET:/Movies/Edit/5

    publicActionResultEdit(intid=0)

    {

    Moviemovie=db.Movies.Find(id);

    if(movie==null)

    {

    returnHttpNotFound();

    }

    returnView(movie);

    }

    //

    //POST:/Movies/Edit/5

    [HttpPost]

    publicActionResultEdit(Moviemovie)

    {

    if(ModelState.IsValid)

    {

    db.Entry(movie).State=EntityState.Modified;

    db.SaveChanges();

    returnRedirectToAction("Index");

    }

    returnView(movie);

    }

    Notice the second Editaction method is preceded by the HttpPostattribute. This attribute specifies that that

    overload of the Editmethod can be invoked only for POST requests. You could apply theHttpGetattribute to the

    first edit method, but that's not necessary because it's the default. (We'll refer to action methods that are implicitly

    mining the Edit Methods and Edit View : Official Microsoft Site http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-m...

    13 20/04/2012 19:41

  • 7/21/2019 6 - Examining the Edit Methods and Edit View _ Official Microsoft Site

    3/13

    assigned the HttpGetattribute as HttpGetmethods.)

    The HttpGetEditmethod takes the movie ID parameter, looks up the movie us ing the Entity FrameworkFind

    method, and returns the selected movie to the Edit view. The ID parameter specifies adefault valueof zero if the

    Editmethod is called without a parameter. If a movie cannot be found, HttpNotFoundis returned. When the

    scaffolding system created the Edit view, it examined theMovieclass and created code to render and

    elements for each property of the class. The following example shows the Edit view that was generated:

    @modelMvcMovie.Models.Movie

    @{

    ViewBag.Title="Edit";

    }

    Edit

  • 7/21/2019 6 - Examining the Edit Methods and Edit View _ Official Microsoft Site

    4/13

    Run the application and navigate to the/MoviesURL. Click an Editlink. In the browser, view the source for the page.

    The HTML for the form element is shown below.

    Movie

  • 7/21/2019 6 - Examining the Edit Methods and Edit View _ Official Microsoft Site

    5/13

    Edit.cshtmlview template take care of displaying appropriate error messages.

    All the HttpGetmethods follow a similar pattern. They get a movie object (or list of objects, in the case ofIndex),

    and pass the model to the view. TheCreatemethod passes an empty movie object to the Create view. All the

    methods that create, edit, delete, or otherwise modify data do so in theHttpPostoverload of the method. Modifying

    data in an HTTP GET method is a security risk, as described in the blog post entryASP.NET MVC Tip #46 Dont use

    Delete Links because they create Security Holes. Modifying data in a GET method also violates HTTP best practices

    and the architectural REST pattern, which specifies that GET requests should not change the state of your application.

    In other words, performing a GET operation should be a safe operation that has no side effects and doesn't modify

    your persisted data.

    Adding a Search Method and Search View

    In this section you'll add a SearchIndexaction method that lets you search movies by genre or name. This will be

    available using the/Movies/SearchIndexURL. The request will display an HTML form that contains input elements that

    a user can fill in in order to search for a movie. When a user submits the form, the action method will get the search

    values posted by the user and use the values to search the database.

    Displaying the SearchIndex Form

    Start by adding a SearchIndexaction method to the existing MoviesControllerclass. The method will return a

    view that contains an HTML form. Here's the code:

    publicActionResult

    SearchIndex(string

    searchString)

    {

    varmovies=frommindb.Movies

    selectm;

    Note about localesIf you normally work with a locale other than English, seeSupporting

    ASP.NET MVC Validation with Non-English Locales.

    mining the Edit Methods and Edit View : Official Microsoft Site http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-m...

    13 20/04/2012 19:41

  • 7/21/2019 6 - Examining the Edit Methods and Edit View _ Official Microsoft Site

    6/13

    if(!String.IsNullOrEmpty(searchString))

    {

    movies=movies.Where(s=>s.Title.Contains(searchString));

    }

    returnView(movies);

    }

    The first line of the SearchIndexmethod creates the following LINQquery to select the movies:

    varmovies=frommindb.Movies

    selectm;

    The query is defined at this point, but hasn't yet been run against the data store.

    If the searchStringparameter contains a string, the movies query is modified to filter on the value of the search

    string, using the following code:

    if(!String.IsNullOrEmpty(searchString))

    {

    movies=movies.Where(s=>s.Title.Contains(searchString));

    }

    LINQ queries are not executed when they are defined or when they are modified by calling a method such asWhere

    or OrderBy. Instead, query execution is deferred, which means that the evaluation of an expression is delayed until its

    realized value is actually iterated over or theToListmethod is called. In the SearchIndexsample, the query is

    executed in the SearchIndex view. For more information about deferred query execution, seeQuery Execution.

    Now you can implement the SearchIndexview that will display the form to the user. Right-click inside the

    SearchIndexmethod and then click Add View. In the Add Viewdialog box, specify that you're going to pass a

    Movieobject to the view template as its model class. In theScaffold templatelist, choose List, then click Add.

    When you click the Addbutton, the Views\Movies\SearchIndex.cshtmlview template is created. Because you selected

    List in the Scaffold templatelist, Visual Web Developer automatically generated (scaffolded) some default content in

    the view. The scaffolding created an HTML form. It examined theMovieclass and created code to render

    elements for each property of the class. The listing below shows the Create view that was generated:

    @modelIEnumerable

    @{

    ViewBag.Title="SearchIndex";

    mining the Edit Methods and Edit View : Official Microsoft Site http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-m...

    13 20/04/2012 19:41

  • 7/21/2019 6 - Examining the Edit Methods and Edit View _ Official Microsoft Site

    7/13

    }

    SearchIndex

    @Html.ActionLink("CreateNew","Create")

    Title

    ReleaseDate

    Genre

    Price

    @foreach(variteminModel){

    @Html.DisplayFor(modelItem=>item.Title)

    @Html.DisplayFor(modelItem=>item.ReleaseDate)

    @Html.DisplayFor(modelItem=>item.Genre)

    @Html.DisplayFor(modelItem=>item.Price)

    @Html.ActionLink("Edit","Edit",new{id=item.ID})|

    @Html.ActionLink("Details","Details",new{id=item.ID})|

    @Html.ActionLink("Delete","Delete",new{id=item.ID})

    }

    Run the application and navigate to/Movies/SearchIndex. Append a query string such as ?searchString=ghostto

    the URL. The filtered movies are displayed.

    mining the Edit Methods and Edit View : Official Microsoft Site http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-m...

    13 20/04/2012 19:41

  • 7/21/2019 6 - Examining the Edit Methods and Edit View _ Official Microsoft Site

    8/13

    If you change the signature of the SearchIndexmethod to have a parameter namedid, the idparameter will match

    the {id}placeholder for the default routes set in theGlobal.asaxfile.

    {controller}/{action}/{id}

    The modified SearchIndexmethod would look as follows:

    publicActionResultSearchIndex(stringid)

    {

    stringsearchString=id;

    var

    movies

    =from

    mindb.Movies

    selectm;

    if(!String.IsNullOrEmpty(searchString))

    {

    movies=movies.Where(s=>s.Title.Contains(searchString));

    }

    returnView(movies);

    }

    You can now pass the search title as route data (a URL segment) instead of as a query string value.

    mining the Edit Methods and Edit View : Official Microsoft Site http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-m...

    13 20/04/2012 19:41

  • 7/21/2019 6 - Examining the Edit Methods and Edit View _ Official Microsoft Site

    9/13

    However, you can't expect users to modify the URL every time they want to search for a movie. So now you you'll add

    UI to help them filter movies. If you changed the signature of theSearchIndexmethod to test how to pass the

    route-bound ID parameter, change it back so that yourSearchIndexmethod takes a string parameter named

    searchString:

    publicActionResultSearchIndex(stringsearchString)

    {

    varmovies=frommindb.Movies

    selectm;

    if(!String.IsNullOrEmpty(searchString))

    {

    movies=movies.Where(s=>s.Title.Contains(searchString));

    }

    returnView(movies);

    }

    Open the Views\Movies\SearchIndex.cshtmlfile, and just after @Html.ActionLink("CreateNew","Create"), add

    the following:

    @using(Html.BeginForm()){

    Title:@Html.TextBox("SearchString")

    }

    The following example shows a portion of theViews\Movies\SearchIndex.cshtmlfile with the added filtering markup.

    @modelIEnumerable

    @{

    ViewBag.Title="SearchIndex";

    }

    SearchIndex

    @Html.ActionLink("CreateNew","Create")

    @using(Html.BeginForm()){

    Title:@Html.TextBox("SearchString")

    }

    mining the Edit Methods and Edit View : Official Microsoft Site http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-m...

    13 20/04/2012 19:41

  • 7/21/2019 6 - Examining the Edit Methods and Edit View _ Official Microsoft Site

    10/13

    The Html.BeginFormhelper creates an opening tag. The Html.BeginFormhelper causes the form to post to

    itself when the user submits the form by clicking theFilterbutton.

    Run the application and try searching for a movie.

    There's no HttpPostoverload of the SearchIndexmethod. You don't need it, because the method isn't changing

    the state of the application, just filtering data.

    You could add the following HttpPostSearchIndexmethod. In that case, the action invoker would match the

    HttpPostSearchIndexmethod, and the HttpPostSearchIndexmethod would run as shown in the image below.

    [HttpPost]

    publicstringSearchIndex(FormCollectionfc,stringsearchString)

    {

    return"From[HttpPost]SearchIndex:"+searchString+"";

    }

    However, even if you add thisHttpPostversion of the SearchIndexmethod, there's a limitation in how this has all

    been implemented. Imagine that you want to bookmark a particular search or you want to send a link to friends thatthey can click in order to see the same filtered list of movies. Notice that the URL for the HTTP POST request is the

    same as the URL for the GET request (localhost:xxxxx/Movies/SearchIndex) -- there's no search information in the URL

    itself. Right now, the search string information is sent to the server as a form field value. This means you can't capture

    that search information to bookmark or send to friends in a URL.

    mining the Edit Methods and Edit View : Official Microsoft Site http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-m...

    de 13 20/04/2012 19:41

  • 7/21/2019 6 - Examining the Edit Methods and Edit View _ Official Microsoft Site

    11/13

    The solution is to use an overload ofBeginFormthat specifies that the POST request should add the search

    information to the URL and that is should be routed to the HttpGet version of theSearchIndexmethod. Replace the

    existing parameterless BeginFormmethod with the following:

    @using(Html.BeginForm("SearchIndex","Movies",FormMethod.Get))

    Now when you submit a search, the URL contains a search query string. Searching will also go to theHttpGet

    SearchIndexaction method, even if you have aHttpPostSearchIndexmethod.

    Adding Search by Genre

    If you added the HttpPostversion of the SearchIndexmethod, delete it now.

    Next, you'll add a feature to let users search for movies by genre. Replace theSearchIndexmethod with the

    following code:

    publicActionResultSearchIndex(stringmovieGenre,stringsearchString)

    {

    varGenreLst=newList();

    varGenreQry=fromdindb.Movies

    orderbyd.Genre

    selectd.Genre;

    GenreLst.AddRange(GenreQry.Distinct());

    ViewBag.movieGenre=newSelectList(GenreLst);

    varmovies=frommindb.Movies

    selectm;

    if(!String.IsNullOrEmpty(searchString))

    {

    movies=movies.Where(s=>s.Title.Contains(searchString));

    }

    mining the Edit Methods and Edit View : Official Microsoft Site http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-m...

    e 13 20/04/2012 19:41

  • 7/21/2019 6 - Examining the Edit Methods and Edit View _ Official Microsoft Site

    12/13

    You must be logged in to leave a comment. Show Comments

    if(string.IsNullOrEmpty(movieGenre))

    returnView(movies);

    else

    {

    returnView(movies.Where(x=>x.Genre==movieGenre));

    }

    }

    This version of the SearchIndexmethod takes an additional parameter, namelymovieGenre. The first few lines of

    code create a Listobject to hold movie genres from the database.

    The following code is a LINQ query that retrieves all the genres from the database.

    varGenreQry=fromdindb.Movies

    orderbyd.Genre

    selectd.Genre;

    The code uses the AddRangemethod of the generic Listcollection to add all the distinct genres to the list. (Without

    the Distinctmodifier, duplicate genres would be added for example, comedy would be added twice in our

    sample). The code then stores the list of genres in theViewBagobject.

    The following code shows how to check themovieGenreparameter. If it's not empty the code further constrains the

    movies query to limit the selected movies to the specified genre.

    if(string.IsNullOrEmpty(movieGenre))

    returnView(movies);

    else

    {

    returnView(movies.Where(x=>x.Genre==movieGenre));

    }

    Adding Markup to the SearchIndex View to Support Search by Genre

    Add an Html.DropDownListhelper to the Views\Movies\SearchIndex.cshtmlfile, just before the TextBoxhelper. The

    completed markup is shown below:

    @Html.ActionLink("CreateNew","Create")

    @using(Html.BeginForm("SearchIndex","Movies",FormMethod.Get)){

    Genre:@Html.DropDownList("movieGenre","All")

    Title:@Html.TextBox("SearchString")

    }

    Run the application and browse to/Movies/SearchIndex. Try a search by genre, by movie name, and by both criteria.

    In this section you examined the CRUD action methods and views generated by the framework. You created a search

    action method and view that let users search by movie title and genre. In the next section, you'll look at how to add a

    property to the Moviemodel and how to add an initializer that will automatically create a test database.

    By Rick Anderson, Rick Anderson works as a programmer writer for Microsoft, focusing

    on ASP.NET MVC, jQuery and Entity Framework. He enjoys working with the top

    contributors in the ASP.NET MVC forum.

    Previous

    Accessing Your Model's Data from a

    Controller

    You're Viewing

    Examining the Edit Methods and Edit

    View

    Next

    Adding a New Field to the Movie

    Model and Table

    Comments (1)

    mining the Edit Methods and Edit View : Official Microsoft Site http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-m...

    de 13 20/04/2012 19:41

  • 7/21/2019 6 - Examining the Edit Methods and Edit View _ Official Microsoft Site

    13/13

    mining the Edit Methods and Edit View : Official Microsoft Site http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-m...