【发布时间】:2021-01-15 11:33:30
【问题描述】:
现在,我正在运行这个 MVC 项目。我有一个包含两个客户的表,它们在不使用数据库的情况下硬编码到表中。下面的两张图片显示了当您单击其中一位客户时应该发生的情况。它将使用操作结果详细信息提取以下 URL“Customers/Details/1”,同时显示该客户的 id。由于只有两个客户,如果将 url 更改为“Customers/Details/3”,则会返回 HttpNotFound 错误。我已经包含了我的客户控制器、表的视图模型和路由配置文件。当我单击其中一位客户时,我得到“未找到视图'详细信息'或其主视图,或者没有视图引擎支持搜索的位置。搜索了以下位置:”。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;
using WebApplication1.ViewModels;
namespace WebApplication1.Controllers
{
public class CustomerController : Controller
{
// GET: customer/getCustomer
public ActionResult Index()
{
//Create instance of movie
var customers = new List<Customer>
{
new Customer {Name = "John Smith"},
new Customer {Name = "Jane Doe"}
};
var viewModel = new RandomMovieViewModel
{
Customers = customers
};
return View(viewModel);
}
public ActionResult details(int id)
{
if (customers == null)
return HttpNotFound();
else
return View(viewModel);
}
}
}
@model WebApplication1.ViewModels.RandomMovieViewModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Customers</h2>
@if (Model.Customers.Count == 0)
{
<text>There are no customers</text>
}
else
{
<table id="customers" class="table table-bordered table-hover">
<thead>
<tr>
<th>Customer</th>
</tr>
<tr>
</thead>
<tbody>
@foreach (var customer in Model.Customers)
{
<tr>
<td>@Html.ActionLink(customer.Name, "details", new {id = 1 }, null)</td>
</tr>
}
</tbody>
</table>
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace WebApplication1
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Customers",
"Customer/details/{id}",
new { controller = "Customer", action = "details", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
【问题讨论】:
-
您能发布
details操作的真实代码吗?您显示的内容无法编译。 -
那是真正的代码,我不知道该为那个actionResult放什么
-
我认为我的 actionResult 详细信息和路线配置有问题
标签: c# asp.net asp.net-mvc asp.net-mvc-4