如何更改网址中的ID

时间:2019-06-06 14:50:02

标签: c# asp.net-mvc razor

我有一个电影网站,它没有在URL中搜索ID。 我的目标是单击(电影的)详细信息按钮,然后选择正确的电影ID并将其放入URL。

例如: http://localhost:54906/Movies/Details1/1

当我单击详细信息按钮时,即使我在控制器中有“(int id)”,它也会始终在URL中搜索0

这是控制器代码:

public ActionResult Details1(int? id)
        {
            MoviesData movies = db.MoviesData.Find(id);

            return View(movies);
        }

这是路线:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");



            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });


        }

视图:

<table class="table table-bordered table-responsive table-hover">
        <tr>
            <th><b>Nome </b></th>
            <th><b>Categoria </b></th>
            <th><b>Ano de Lançamento </b></th>
            <th><b>Descrição do Filme </b></th>
            <th></th>


        </tr>
        @foreach (var item in Model)
        {
        <tr>
            <td>@item.MovieName</td>
            <td>@item.MovieCategory</td>
            <td>@item.MovieYear</td>
            <td>@item.MovieDescription</td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.MovieID }) |
                @Html.ActionLink("Detalhes do Filme", "Details1", new { id = item.MovieID }) |
                @Html.ActionLink("Eliminar", "Delete", new { id = item.MovieID })
            </td>
        </tr>
        }
    </table>

如果我在网址中输入了正确的ID,它会正确地放置在正确的位置,但是永远不会

编辑(添加HTML结果):

<td>
                <a href="/Movies/Edit/0">Edit</a> |
                <a href="/Movies/Details1/0">Movie Details</a> |
                <a href="/Movies/Delete/0">Eliminar</a>
            </td>

编辑2(添加模型和控制器):

这是负责电影的模特:

 public class MovieViewModel
    {
        public int MovieID { get; set; }
        public string MovieName { get; set; }
        public string MovieDescription { get; set; }
        public string MovieCategory { get; set; }
        public string MovieYear { get; set; }
    }

这是完整的视图:

@model WebApplication3.Models.MoviesData

@{
    ViewBag.Title = "Details1";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Details1</h2>

<div>
    <h4>MoviesData</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            Movie Name  

        </dt>

        <dd>
            @Html.DisplayFor(model => model.MovieName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.MovieDescription)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.MovieDescription)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.MovieCategory)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.MovieCategory)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.MovieYear)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.MovieYear)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.MovieID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

这是负责Details1视图的控制器:

public ActionResult Details1(int? id)
        {
            MoviesData movies = db.MoviesData.Find(id);

            return View(movies);
        }

编辑3(添加索引类Controller)

public ActionResult Index(string searchBy, string search)
        { 
            MovieViewModel[] movies = db.MoviesData.Select(movie => new MovieViewModel
            {
                MovieID = movie.MovieID,
                MovieName = movie.MovieName,
                MovieDescription = movie.MovieDescription,
                MovieCategory = movie.MovieCategory,
                MovieYear = movie.MovieYear
            }).ToArray();

            if (searchBy == "Categoria")
            {
                return View(movies.Where(x => x.MovieCategory==search || search == null).ToList());
            }
            else
            {
                return View(movies.Where(x => x.MovieName != null && (search == null || x.MovieName.StartsWith(search))).ToList());
            }
        }

搜索内容与搜索按钮有关

0 个答案:

没有答案