C# C MVC-将URL参数传递给方法
在MVC项目中将url参数传递给我的方法时遇到问题。 当前遇到以下错误: 参数字典包含“mybloggywog.Controllers.PostController”中方法“System.Web.Mvc.ActionResult SinglePostInt32”的不可为null类型“System.Int32”的参数“id”的null条目。可选参数必须是引用类型、可为null的类型或声明为可选参数 电话如下:C# C MVC-将URL参数传递给方法,c#,asp.net-mvc,C#,Asp.net Mvc,在MVC项目中将url参数传递给我的方法时遇到问题。 当前遇到以下错误: 参数字典包含“mybloggywog.Controllers.PostController”中方法“System.Web.Mvc.ActionResult SinglePostInt32”的不可为null类型“System.Int32”的参数“id”的null条目。可选参数必须是引用类型、可为null的类型或声明为可选参数 电话如下: @{ foreach (var article in ViewBag.Disp
@{
foreach (var article in ViewBag.DisplayPosts)
{
<a class="ArticleLink" href="@Url.Action("SinglePost", "Post", article.Id)">@article.Title</a>
<p>@article.CreatedOn</p>
<br/>
}
}
方法:
public ActionResult SinglePost(int id)
{
var entities = new blogEntities();
var post = entities.Set<Post>();
var postToDisplay = (from p in post
where p.Id == id
select p);
ViewBag.SinglePost = postToDisplay;
return View();
}
尝试将其更改为匿名对象:
@Url.Action("SinglePost", "Post", new { id = article.Id })
这很有效。我还不能投票,但我向你致敬,先生。
@Url.Action("SinglePost", "Post", new { id = article.Id })