将参数从@Html.ActionLink MVC 4 传递给控制器 [英] Pass parameter to controller from @Html.ActionLink MVC 4

查看:35
本文介绍了将参数从@Html.ActionLink MVC 4 传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这一行:

@Html.ActionLink("Reply", "BlogReplyCommentAdd", "Blog",
         new { blogPostId = blogPostId, replyblogPostmodel = Model,
         captchaValid = Model.AddNewComment.DisplayCaptcha })

我在 blogPostId 上收到以下运行时错误:

I get the following runtime error on blogPostId:

参数字典包含一个空的参数条目方法的不可为空类型System.Int32"的blogPostId"'System.Web.Mvc.ActionResult BlogReplyCommentAdd(Int32,Nop.Web.Models.Blogs.BlogPostModel, Boolean)' in'Nop.Web.Controllers.BlogController'.可选参数必须是引用类型、可为空类型或声明为可选范围.参数名称:参数

The parameters dictionary contains a null entry for parameter 'blogPostId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult BlogReplyCommentAdd(Int32, Nop.Web.Models.Blogs.BlogPostModel, Boolean)' in 'Nop.Web.Controllers.BlogController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

我已经在顶部为此分配了一个值,例如

I have already assign a value for this on top such as

    @{         
        var blogPostId = Model.Id;          
     }

我的控制器:

 public ActionResult BlogReplyCommentAdd(int blogPostId, BlogPostModel model, bool captchaValid)
    {}

我做错了吗?请举个例子.

Am I doing something wrong? Please give me an example.

推荐答案

您使用了错误的 Html.ActionLink 帮助器.你以为的routeValues其实是htmlAttributes!只需查看生成的 HTML,您就会发现该锚点的 href 属性与您预期的不同.

You are using a wrong overload of the Html.ActionLink helper. What you think is routeValues is actually htmlAttributes! Just look at the generated HTML, you will see that this anchor's href property doesn't look as you expect it to look.

这是您正在使用的:

@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
)

此外,您的代码还有一个非常严重的问题.以下路由值:

Also there's another very serious issue with your code. The following routeValue:

replyblogPostmodel = Model

您不可能在 ActionLink 中传递这样的复杂对象.因此,摆脱它并从控制器操作中删除 BlogPostModel 参数.您应该使用 blogPostId 参数从保留此模型的任何位置检索模型,或者如果您更愿意从在 GET 操作中检索模型的任何位置检索模型:

You cannot possibly pass complex objects like this in an ActionLink. So get rid of it and also remove the BlogPostModel parameter from your controller action. You should use the blogPostId parameter to retrieve the model from wherever this model is persisted, or if you prefer from wherever you retrieved the model in the GET action:

public ActionResult BlogReplyCommentAdd(int blogPostId, bool captchaValid)
{
    BlogPostModel model = repository.Get(blogPostId);
    ...
}

至于您最初的问题与错误的重载有关,我建议您使用命名参数编写助手:

As far as your initial problem is concerned with the wrong overload I would recommend you writing your helpers using named parameters:

@Html.ActionLink(
    linkText: "Reply",
    actionName: "BlogReplyCommentAdd",
    controllerName: "Blog",
    routeValues: new {
        blogPostId = blogPostId, 
        captchaValid = Model.AddNewComment.DisplayCaptcha
    },
    htmlAttributes: null
)

现在不仅您的代码更具可读性,而且您永远不会在 Microsoft 为这些帮助程序制作的无数重载之间产生混淆.

Now not only that your code is more readable but you will never have confusion between the gazillions of overloads that Microsoft made for those helpers.

这篇关于将参数从@Html.ActionLink MVC 4 传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆