简体   繁体   English

索引视图未将参数 Id 传递给 mvc controller

[英]Index view not passing parameter Id to mvc controller

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).我正在使用 asp.net 核心 MVC 项目,并且在尝试从视图 (cshtml) 中获取我的 model 的 ID API 时遇到问题。 (Please note the getbyid API works when testing with Postman) (请注意 getbyid API 在使用 Postman 进行测试时有效)

My Model:我的 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:我的 controller 方法:

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)我的存储库(这是我调试后得到 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" src=@Html.DisplayFor(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注意:我认为我的 ActionLink 中的 routeValues 没有得到我需要的 ID

ActionLink is always calling GET metod ActionLink 总是调用 GET 方法

So fix your action所以修正你的行动

[Route("Delete/{id}")]
public ActionResult Delete( int id)
.....

the same for others similar APIs其他类似的 API 也一样

[Route("ApplyDelete/{id}")]
public ActionResult ApplyDelete(int id)
....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM