【发布时间】:2019-04-03 17:26:16
【问题描述】:
我有一个包含项目列表的视图 每个项目都有一个重定向到项目页面的操作链接
观点:
<div class="row">
@foreach (var item in Model)
{
<div class="card text-white bg-secondary mb-3" style="max-width: 400px;">
<img class="card-img-top" src="@ViewBag.Path" alt="Card image" style="width:100%">
<div class="card-body">
<h4 class="card-title">@item.ArticleTitle</h4>
<a onclick="location.href='@Url.Action("Article", "Article", new { Controller = "Account", Action = "SignIn", id = item.ArticleID })'" class="btn btn-primary stretched-link">إقرأ المزيد »»</a>
</div>
</div>
}
</div>
这个动作链接的结果是/Article/Article/4 因为 4 是 db item id
必须显示每个项目详细信息页面的另一个视图:
@model IEnumerable<ElMatrodySite.Models.NewsData>
<h2>@Html.DisplayNameFor(mode => mode.ArticleTitle)</h2>
<img src=@ViewBag.Path style="max-width:40%;max-height:400px;" />
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ArticleTitle)
</th>
<th>
@Html.DisplayNameFor(model => model.ArticleText)
</th>
<th>
@Html.DisplayNameFor(model => model.ArticlePostDate)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ArticleTitle)
</td>
<td>
@Html.DisplayFor(modelItem => item.ArticleText)
</td>
<td>
@Html.DisplayFor(modelItem => item.ArticlePostDate)
</td>
<td>
@Html.ActionLink("EditArticle", "Article", new { id = item.ArticleID }) |
@using (Html.BeginForm("Delete", "Article", FormMethod.Post))
{
@Html.ActionLink("Delete", "Article", new { id = item.ArticleID })
}
</td>
</tr>
}
</table>
控制器:
[HttpGet]
[AllowAnonymous]
public ActionResult Article(int ArtID)
{
using (MatrodyEntities db = new MatrodyEntities())
{
db.NewsData.Find(ArtID);
return View("Article", ArtID);
}
}
[AllowAnonymous]
public ActionResult Articles()
{
List<NewsData> articles = new List<NewsData>();
using (MatrodyEntities db = new MatrodyEntities())
{
var type = new NewsData();
articles = db.NewsData.Where(xn => xn.ArticleID == type.ArticleID).ToList();
ViewBag.Count = db.NewsData.SqlQuery("SELECT * FROM dbo.NewsData").Count();
return View(from NewsData in db.NewsData.ToList() select NewsData);
}
}
之后每次当我访问类似这篇文章/文章/4 的链接时 它不起作用,它向我显示此错误消息:
参数字典包含“ElMatrodySite.Controllers.ArticleController”中方法“System.Web.Mvc.ActionResult Article(Int32)”的不可为空类型“System.Int32”的参数“ArtID”的空条目。可选参数必须是引用类型、可空类型或声明为可选参数。 参数名称:参数
System.ArgumentException:参数字典包含“ElMatrodySite.Controllers”中方法“System.Web.Mvc.ActionResult Article(Int32)”的不可为空类型“System.Int32”的参数“ArtID”的空条目。文章控制器”。可选参数必须是引用类型、可空类型或声明为可选参数。 参数名称:参数
路由配置:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace ElMatrodySite
{
public class RouteConfig
{
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 }
);
}
}
}
【问题讨论】:
-
它显示错误消息,因为
ArtID为空 -
将 public ActionResult Article(int ArtID) 更改为 public ActionResult Article(int id) 并更正 إقرأ المزيد »» to إقرأ المزيد »»
-
这行代码有什么用
articles = db.NewsData.Where(xn => xn.ArticleID == type.ArticleID).ToList();因为你没有在任何地方使用文章 -
@Rashedul.Rubel 我按照你说的做了,它给了我错误消息`传递到字典中的模型项是'System.Int32'类型,但是这个字典需要一个类型的模型项'System.Collections.Generic.IEnumerable
1[ElMatrodySite.Models.NewsData]'. -
@GabrielCostin 它在我上面提到的第一个视图中显示了一个包含数据库项目的列表
标签: c# sql-server asp.net-mvc