Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to modify an ASP.NET MVC 5 web application.
The index currently shows all the movies on my database, the entity "Movie" is my model.

I want to use Ajax to implement a partial view in the Index, in order to show a search form to filter the movies, results will be filtered based on the input parameters "Title" and "Director".

My implementation doesn't work: after entering the required parameters in the text fields of the form, I click on the "Search" button but the movies are not filtered, I still see all of them.

I debugged it and noticed that the Ajax Request is not recognized, the statements inside the following conditional expression are not be read:

if(Request.IsAjaxRequest())


What I have tried:

-----------------------------------
Controller:

public ActionResult Index(string selectedTitle, string selectedDirector)
     {
         List<Movie> movies= _movieProxy.GetAll();

         var movies= from singleMovie in movies
                      select singleMovie ;

         if(Request.IsAjaxRequest())
         {
             movies= _movieProxy.GetMovieByProperties(selectedTitle, selectedDirector);

             movie= from movieSearch in movies
                    where (string.IsNullOrEmpty(selectedTitle)
                    || movieSearch .Title.ToLower().Contains(selectedTitle.ToLower()))
                    && ((string.IsNullOrEmpty(selectedDirector)
                    ||
                    movieSearch.Director.ToLower().Contains(selectedDirector.ToLower()))
                    select movieSearch ;
             return PartialView("_Index", movie);
           }

           return View(movie);
       }


-------------------------------------------------------------
Index View:

@model IEnumerable<DataModel.Movie>

@using (Ajax.BeginForm(new AjaxOptions()
{
    HttpMethod = "get",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "Search"
}))
{
   
    <div class="form-inline" >
        <label>Title</label>
        <input type="search" name="searchTitle" />
        <label>Director</label>
        <input type="search" name="searchDirector" />
     </div>
}
<table class="table">
    
<div id="Search"

    @foreach (var item in Model)
    {
        <p>
            @Html.Partial("_Index", Model)
        </p>

        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Director)
            </td>            
        </tr>
    }

    </div>
</table>


--------------------------------------------------------------
_Index (partial view):

C#
@model IEnumerable<DataModel.Movie>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Director)
        </th>
   </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Director)
        </td>
     </tr>
}

</table>
Posted
Updated 29-Nov-20 22:05pm
v4

1 solution

Start by applying your filter consistently:
C#
public ActionResult Index(string selectedTitle = null, string selectedDirector = null)
{
    IEnumerable<Movie> movies = _movieProxy.GetAll();
    if (!string.IsNullOrEmpty(selectedTitle))
    {
        movies = movies.Where(m => m.Title.IndexOf(selectedTitle, StringComparison.CurrentCultureIgnoreCase) != -1);
    }
    if (!string.IsNullOrEmpty(selectedDirector))
    {
        movies = movies.Where(m => m.Director.IndexOf(selectedDirector, StringComparison.CurrentCultureIgnoreCase) != -1);
    }
    
    if (Request.IsAjaxRequest())
    {
        return PartialView("_Index", movies);
    }

    return View(movies);
}
Then make sure you have the "unobtrusive AJAX" Javascript library referenced properly in your view.

If it still doesn't work, use your browser's developer tools to ensure that the form is being submitted via AJAX, rather than reloading the page.

In order for the IsAjaxRequest method to return true, the AJAX request needs to have the X-Requested-With header set to XMLHttpRequest.
 
Share this answer
 
Comments
xhon 30-Nov-20 12:38pm    
Thank you for answering. Can you please explain to me the meaning of this conditional expression? -->

m => m.Title.IndexOf(selectedTitle, StringComparison.CurrentCultureIgnoreCase) != -1)
Richard Deeming 30-Nov-20 12:45pm    
s1.IndexOf(s2) != -1 is equivalent to s1.Contains(s2).

There is no option to perform a case-insensitive Contains search on a string, but there is an option to perform a case-insensitive IndexOf search.

Performing a case-insensitive IndexOf will perform better than changing the case of both strings.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900