Reputation: 47
I'm working with asp.net core MVC project and I have an issue when trying to get by Id API of my model from view (cshtml). (Please note the getbyid API works when testing with Postman)
My Model:
public class Movie
{
[Key]
public int Id { get; set; }
public string Title { get; set; }
public string Director { get; set; }
public DateTime DateReleased { get; set; }
[Range(0, 9)]
public int Rating { get; set; }
public string Image { get; set; }
}
My controller methods:
namespace MovieTheatreManagement.Controllers
{
[Route("api/Movie")]
[ApiController]
public class MovieController : Controller
{
#region Fields
private readonly IMovieRepository _movieRepository;
private readonly IMapper _mapper;
private readonly ILogger _logger;
#endregion Fields
#region Constructors
public MovieController(IMovieRepository movieRepository, IMapper mapper, ILogger<MovieController> logger)
{
_movieRepository = movieRepository;
_mapper = mapper;
_logger = logger;
}
#endregion Constructors
#region HtmlRequests
[HttpGet("/")]
public IActionResult Index()
{
return View(_movieRepository.GetAllMovies().ToList().OrderBy(m => m.Id));
}
public ActionResult Create()
{
return View();
}
[HttpGet("Delete")]
[Route("Delete/{id}")]
public ActionResult Delete([FromRoute] int id)
{
var movie = _movieRepository.GetMovieById(id);
return View(movie);
}
#endregion HtmlRequests
[HttpGet("/All")]
public ActionResult<IEnumerable<GetModels>> GetAllMovies()
{
var movieItems = _movieRepository.GetAllMovies();
// var newitems = Convert.FromBase64String();
return Ok(_mapper.Map<IEnumerable<GetModels>>(movieItems));
}
[HttpPost]
public ActionResult Create([FromForm]CreateModels movieRequest)
{
//CreateModels movieRequest = new CreateModels();
_logger.LogInformation("hi I am create method");
var movie = _mapper.Map<Movie>(movieRequest);
_movieRepository.Create(movie);
_movieRepository.SaveChanges();
var command = _mapper.Map<GetModels>(movie);
return RedirectToAction("Index");
//CreatedAtRoute(nameof(GetMovieById), new {Id = command.Id}, command);
}
[HttpPost("id")]
public ActionResult ApplyDelete([FromRoute(Name = "id")]int id)
{
var command = _movieRepository.GetMovieById(id);
if (command == null)
{
return NotFound();
}
_movieRepository.DeleteMovie(command);
_movieRepository.SaveChanges();
return RedirectToAction("Index");
}
My Repository (Here is the issue where after I debug I am getting the id null)
public Movie GetMovieById(int id)
{
return _context.Movies.FirstOrDefault(item => item.Id == id);
}
View:
@model IEnumerable<MovieTheatreManagement.Models.Movie>
@{
ViewBag.Title = "Index";
}
<h2>Movie Theatre Management</h2>
<p>
@Html.ActionLink("Create New Movie", "Create", "Movie", new{}, new { @class = "btn btn-default" })
</p>
<p>
@using (Html.BeginForm())
{
<div>
@Html.TextBox("txtTitle", "")
<input type="submit" value="Filter by Title" class="btn btn-default" />
</div>
}
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Id)
</th>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.DateReleased)
</th>
<th>
@Html.DisplayNameFor(model => model.Director)
</th>
<th>
@Html.DisplayNameFor(model => model.Rating)
</th>
<th>
@Html.DisplayNameFor(model => model.Image)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateReleased)
</td>
<td>
@Html.DisplayFor(modelItem => item.Director)
</td>
<td>
@Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
<img width="100px" height="100px" [email protected](modelItem => item.Image) />
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Delete Movie", "Delete", "Movie" ,new { id = item.Id })
</td>
</tr>
}
</table>
Delete page:
@model MovieTheatreManagement.Models.Movie
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Movie</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Title)
</dt>
<dd>
@Html.DisplayFor(model => model.Title)
</dd>
<dt>
@Html.DisplayNameFor(model => model.DateReleased)
</dt>
<dd>
@Html.DisplayFor(model => model.DateReleased)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Director)
</dt>
<dd>
@Html.DisplayFor(model => model.Director)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Rating)
</dt>
<dd>
@Html.DisplayFor(model => model.Rating)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Image)
</dt>
<dd>
@Html.DisplayFor(model => model.Image)
</dd>
</dl>
@using (Html.BeginForm(actionName:"ApplyDelete", controllerName:"Movie", new { id = Model.Id } , FormMethod.Post)) {
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Delete" class="btn btn-default"/>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</div>
Note: I think the routeValues in my ActionLink is not getting me the ID that I need
Upvotes: 1
Views: 584
Reputation: 43860
ActionLink is always calling GET metod
So fix your action
[Route("Delete/{id}")]
public ActionResult Delete( int id)
.....
the same for others similar APIs
[Route("ApplyDelete/{id}")]
public ActionResult ApplyDelete(int id)
....
Upvotes: 1