Asp.net core mvc 3.1 i have a index view that shows a list of movies, where i added a search text box, and i would like to know if there is any way to pass that search value to the anchor element. This anchor element points to an action that generates a pdf view. But i would like to pass that search input to an anchor so i can query based on that input, and generate diferent pdf results base on that searched input... I would really appreciated if you guys could help me...
MoviesController
// GET: Movies
public async Task<IActionResult> Index(string search)
{
var movies = _context.Movies.Include(m => m.Genre).OrderBy(m=>m.Name);
if (!string.IsNullOrEmpty(search))
{
var query = _context.Movies.Include(m => m.Genre);
var searchedItems = query.Where(m => m.Name.Contains(search) || m.Genre.Name.Contains(search) || m.Year.ToString().Contains(search));
return View(await searchedItems.ToListAsync());
}
return View( await movies.ToListAsync());
}
public IActionResult GeneratePdf(string linkedSearch)
{
var movies = _context.Movies.Include(m => m.Genre).OrderBy(m => m.Name).ToList();
return new ViewAsPdf("GeneratePdf", movies)
{
PageOrientation = Rotativa.AspNetCore.Options.Orientation.Portrait,
PageSize = Rotativa.AspNetCore.Options.Size.A4
};
}
index.cshtml
@model IEnumerable<MovieListApp.Models.Movie>
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-action="Create">Add new movie</a>
</p>
<a asp-action="Index">Refresh</a>
@* Table Omitted for brevity *@
<form asp-action="Index" asp-controller="Movies" method="get">
<p>
Search:<input type="text" name="search" placeholder="name, Genre, year" />
<input type="submit" value="Filtrar" />
</p>
</form>
<a asp-action="GeneratePdf" asp-controller="Movies">Generate Pdf</a>
CodePudding user response:
The anchor tag helper supports passing query parameters using the asp-route-{value}
attribute. But first you need to pass the search value to the view. For that you can use the ViewData
dictionary.
public async Task<IActionResult> Index(string search)
{
// pass the search string to the view so that the anchor can pass it to the GeneratePdf action
ViewData["Search"] = search;
// rest of the code
}
Then inside the view:
<a asp-action="GeneratePdf" asp-controller="Movies" asp-route-linkedSearch="@ViewData["Search"]">Generate Pdf</a>
CodePudding user response:
it is worth mentioning that pdf views are generate with rotativa version 1.2 , the GeneratePdf action method links to a view... MoviesContrller
// GET: Movies
public async Task<IActionResult> Index(string search)
{
ViewData["Search"] = search;
var movies = _context.Movies.Include(m => m.Genre).OrderBy(m=>m.Name);
if (!string.IsNullOrEmpty(search))
{
var query = _context.Movies.Include(m => m.Genre);
var searchedItems = query.Where(m => m.Name.Contains(search) || m.Genre.Name.Contains(search) || m.Year.ToString().Contains(search));
return View(await searchedItems.ToListAsync());
}
return View( await movies.ToListAsync());
}
public async Task<IActionResult> GeneratePdf(string linkedSearch)
{
var movies = _context.Movies.Include(m => m.Genre).OrderBy(m => m.Name);
if (!string.IsNullOrEmpty(linkedSearch))
{
var query = _context.Movies.Include(m => m.Genre);
var searchedItems = query.Where(m => m.Name.Contains(linkedSearch) || m.Genre.Name.Contains(linkedSearch) || m.Year.ToString().Contains(linkedSearch));
return new ViewAsPdf("GeneratePdf",await searchedItems.ToListAsync())
{
PageOrientation = Rotativa.AspNetCore.Options.Orientation.Portrait,
PageSize = Rotativa.AspNetCore.Options.Size.A4
};
}
return new ViewAsPdf("GeneratePdf", await movies.ToListAsync())
{
PageOrientation = Rotativa.AspNetCore.Options.Orientation.Portrait,
PageSize = Rotativa.AspNetCore.Options.Size.A4
};
}
index.cshtml
@model IEnumerable<MovieListApp.Models.Movie>
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-action="Create">Add new movie</a>
</p>
<a asp-action="Index">Refresh</a>
<form asp-action="Index" asp-controller="Movies" method="get">
<p>
Search:<input type="text" name="search" placeholder="name, Genre, year" />
<input type="submit" value="Filtrar" />
</p>
</form>
@{
<table >
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Year)
</th>
<th>
@Html.DisplayNameFor(model => model.Rating)
</th>
<th>
<label>Genre</label>>
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Year)
</td>
<td>
@Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre.Name)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.MovieId">Edit</a> |
<a asp-action="Details" asp-route-id="@item.MovieId">Details</a> |
<a asp-action="Delete" asp-route-id="@item.MovieId">Delete</a>
</td>
</tr>
}
</tbody>
</table>
}
<a asp-action="GeneratePdf" asp-controller="Movies" asp-route-linkedSearch="@ViewData["Search"]">Generate Pdf</a>