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

查看:226
本文介绍了从@ 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:

参数词典共包含参数无效项
  blogPostId非可空类型'System.Int32'对方法
  System.Web.Mvc.ActionResult BlogReplyCommentAdd(的Int32,
  Nop.Web.Models.Blogs.BlogPostModel,布尔值)中
  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.

推荐答案

您正在使用一个错误的过载<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink%28v=vs.108%29.aspx\"><$c$c>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.

下面是你使用的是什么:

Here's what you are using:

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

和这里是你应该用什么:

and here's what you should use:

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

也有与您的code另外一个非常严重的问题。以下routeValue:

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

replyblogPostmodel = Model

您不可能传递一个ActionLink的这样复杂的对象。因此,摆脱它,也删除您的控制器动作 BlogPostModel 参数。您应该使用 blogPostId 参数检索从哪里这种模式是持久化模型,或者无论你检索在GET操作模式,如果你preFER:

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
)

现在不仅是你的code是更具可读性,但你永远不会有重载,微软为那些助手做的gazillions之间的混淆。

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天全站免登陆