C#MVC 5 HTML.ActionLInk

西奥·格里尔

我正在通过Udemy上的C#MVC 5应用程序进行工作,而我一直无法使用Html.ActionLink从视图中调用方法。我尝试过传递客户对象,然后决定尝试传递id。

由于我不知道/不知道的原因,这在显示正确的网址(/ CustomerController / CustomerView / 2)时抛出了HTTP 404错误。这是我的代码:

RouteConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

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

        routes.MapMvcAttributeRoutes();

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

CustomerController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vidly.Models;

namespace Vidly.Controllers
{
public class CustomerController : Controller
{

    private List<CustomerModels> customers = new List<CustomerModels>
        {
            new CustomerModels {Id = 0, Name = "Theo Greer" },
            new CustomerModels {Id = 1, Name = "Mark Pate" },
            new CustomerModels {Id = 2, Name = "Jerry Jones" },
            new CustomerModels {Id = 3, Name = "Mary Alexander" },
            new CustomerModels {Id = 4, Name = "Patricia Smith" }
        };

    // GET: Customer
    public ActionResult Index()
    {
        return View(customers);
    }

    public ActionResult CustomerView(int id)
    {
        CustomerModels tempCust = customers.FirstOrDefault(CustomerModels => CustomerModels.Id == id);
        return View(tempCust);
    }
}
}

Index.cshtml

@model List<Vidly.Models.CustomerModels>
@{ }
<h2>Customers</h2>

<table class="table table-bordered table-hover">
<tr>
    <th>Customer</th>
</tr>
@foreach (var customer in Model)
{
    <tr><td>@Html.ActionLink(customer.Name, "CustomerView", "CustomerController", new { id = customer.Id }, null)</td></tr>
}

当我单击表中的链接时,将引发http 404错误。非常感谢您的宝贵时间。

pgcan

我认为适当的URI应该是(/ Customer / CustomerView / 2),而不应该是(/ Customer Controller / CustomerView / 2)。

下面是正确的代码行。

@ Html.ActionLink(customer.Name,“ CustomerView”,“ Customer”,新{id = customer.Id},null)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

MVC 5 for..loop ActionLink 破坏 html

来自分类Dev

C#MVC 5将html视图转换为PDF以在Bussness Layer中发送电子邮件附件

来自分类Dev

C#MVC 5将html视图转换为PDF以在Bussness Layer中发送电子邮件附件

来自分类Dev

在C#MVC中压缩/压缩HTML

来自分类Dev

ASP.NET MVC5通过单击Html.ActionLink更改语言/区域性

来自分类Dev

在Html.ActionLink(MVC 4)中使用Html.DisplayNameFor

来自分类Dev

MVC @ html.Actionlink包含html i标签

来自分类Dev

MVC @ html.Actionlink包括html i标签

来自分类Dev

ActionLink扩展方法MVC5中的htmlAttributes

来自分类Dev

如何使用ActionLink MVC 5执行以下sql查询?

来自分类Dev

在ASP.NET MVC 5 ActionLink中包含锚标记

来自分类Dev

在MVC 4的Html.ActionLink中附加“ id =“参数

来自分类Dev

MVC中的CSS样式以显示不同的@ Html.ActionLink链接

来自分类Dev

在MVC 4的Html.ActionLink中附加“ id =“参数

来自分类Dev

C#MVC Html.CheckBoxFor不绑定到模型

来自分类Dev

HTML.ActionLink扩展

来自分类Dev

HTML.Actionlink类

来自分类Dev

Asp.Net mvc 5-如何在Html.ActionLink()中传递复杂对象作为路由值,以便默认模型绑定程序可以映射它?

来自分类Dev

表MVC中的ActionLink

来自分类Dev

MVC ActionLink建议

来自分类Dev

MVC 路由 - actionlink

来自分类Dev

ASP.NET MVC:Html.Action / Html.ActionLink和区域性特定的DateTime

来自分类Dev

HTML.ActionLink动态参数

来自分类Dev

ViewBag的Html.ActionLink值

来自分类Dev

Html.ActionLink的CSS图像

来自分类Dev

HTML.ActionLink动态参数

来自分类Dev

C#MVC Razor Html.ValidationSummary将消息显示为HTML

来自分类Dev

MVC 5使用ActionLink将模型从视图传递到控制器

来自分类Dev

如何在ASP.NET MVC 5中通过ActionLink传递包含连字符的routeValues

Related 相关文章

热门标签

归档