在这一行中:
@Html.ActionLink("Reply", "BlogReplyCommentAdd", "Blog",
new { blogPostId = blogPostId, replyblogPostmodel = Model,
captchaValid = Model.AddNewComment.DisplayCaptcha })
我在blogPostId上得到以下运行时错误:
对于'Nop.Web.Controllers.BlogController‘中的方法'System.Web.Mvc.ActionResult BlogReplyCommentAdd(Int32,Nop.Web.Models.Blogs.BlogPostModel,Boolean)’,参数字典包含不可为null类型为'System.Int32‘的参数'blogPostId’的null项。可选参数必须是引用类型、可以为must的类型或声明为可选参数。参数名称:参数
我已经在上面为它赋值了,比如
@{
var blogPostId = Model.Id;
}
我的控制器:
public ActionResult BlogReplyCommentAdd(int blogPostId, BlogPostModel model, bool captchaValid)
{}
我做错了什么吗?请举个例子吧。
发布于 2013-01-04 15:18:08
您正在使用错误的Html.ActionLink
帮助器重载。你认为的routeValues
实际上就是htmlAttributes
!只需查看生成的HTML,您就会发现这个锚的href属性看起来并不像您期望的那样。
下面是您正在使用的内容:
@Html.ActionLink(
"Reply", // linkText
"BlogReplyCommentAdd", // actionName
"Blog", // routeValues
new { // htmlAttributes
blogPostId = blogPostId,
replyblogPostmodel = Model,
captchaValid = Model.AddNewComment.DisplayCaptcha
}
)
下面是你应该使用的:
@Html.ActionLink(
"Reply", // linkText
"BlogReplyCommentAdd", // actionName
"Blog", // controllerName
new { // routeValues
blogPostId = blogPostId,
replyblogPostmodel = Model,
captchaValid = Model.AddNewComment.DisplayCaptcha
},
null // htmlAttributes
)
此外,您的代码还有另一个非常严重的问题。以下routeValue:
replyblogPostmodel = Model
您不可能在ActionLink中传递像这样的复杂对象。所以去掉它,同时从你的控制器操作中删除BlogPostModel
参数。您应该使用blogPostId
参数从保存此模型的位置检索模型,或者如果您愿意,从GET操作中检索模型的位置检索模型:
public ActionResult BlogReplyCommentAdd(int blogPostId, bool captchaValid)
{
BlogPostModel model = repository.Get(blogPostId);
...
}
至于你最初的问题是关于错误的重载,我建议你使用命名参数来编写你的助手:
@Html.ActionLink(
linkText: "Reply",
actionName: "BlogReplyCommentAdd",
controllerName: "Blog",
routeValues: new {
blogPostId = blogPostId,
captchaValid = Model.AddNewComment.DisplayCaptcha
},
htmlAttributes: null
)
现在,不仅你的代码更具可读性,而且你永远不会对微软为这些帮助器创建的无数重载感到困惑。
发布于 2016-08-24 20:03:40
我必须传递两个参数,例如:
/控制器/操作/参数1Value/参数2Value
这样:
@Html.ActionLink(
linkText,
actionName,
controllerName,
routeValues: new {
Param1Name= Param1Value,
Param2Name = Param2Value
},
htmlAttributes: null
)
将生成此url。
/Controller/Action/Param1Value?Param2Name=Param2Value
我使用了一种变通方法,通过合并参数1中的参数2,我得到了我想要的:
@Html.ActionLink(
linkText,
actionName,
controllerName,
routeValues: new {
Param1Name= "Param1Value / Param2Value" ,
},
htmlAttributes: null
)
我得到了:
/Controller/Action/Param1Value/Param2Value
发布于 2019-05-19 14:03:17
您可以使用下面的代码来传递值。
@Html.ActionLink("About", "About", "Home",new { name = ViewBag.Name }, htmlAttributes:null )
控制器:
public ActionResult About(string name)
{
ViewBag.Message = "Your application description page.";
ViewBag.NameTransfer = name;
return View();
}
URL看起来像这样
http://localhost:50297/Home/About?name=My%20Name%20is%20Vijay
https://stackoverflow.com/questions/14152575
复制相似问题