ASP.NET MVC - 传递参数给控制器 [英] ASP.NET MVC - passing parameters to the controller

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

问题描述

我有一个动作方法的控制器如下:

I have a controller with an action method as follows:

public class InventoryController : Controller
{
    public ActionResult ViewStockNext(int firstItem)
    {
        // Do some stuff
    }
}

当我运行它,我得到一个错误,说明:

And when I run it I get an error stating:

参数字典不包含类型'System.Int32'参数'与firstItem的有效价值。为了使参数可选其类型要么是引用类型或可空类型。

The parameters dictionary does not contain a valid value of type 'System.Int32' for parameter 'firstItem'. To make a parameter optional its type should either be a reference type or a Nullable type.

我是有在一个点工作,我决定尝试功能,无需参数。找出控制器不持续性,我把参数回来,现在它拒绝当我打电话的方法来识别参数。

I had it working at one point and I decided to try the function without parameters. Finding out that the controller was not persistant I put the parameter back in, now it refuses to recognise the parameter when I call the method.

我使用此URL语法来调用的行动:

I'm using this url syntax to call the action:

http://localhost:2316/Inventory/ViewStockNext/11

任何想法,为什么我会得到这个错误,我需要做修复它是什么?

Any ideas why I would get this error and what I need to do to fix it?

我已经尝试添加另一个需要它也失败同样的原因类整数方法。我已经尝试添加一个接受一个字符串,字符串被设置为null。我试着添加一个不带参数和工作得很好,但当然不会满足我的需求。

I've tried adding another method that takes an integer to the class it it also fails with the same reason. I've tried adding one that takes a string, and the string is set to null. I've tried adding one without parameters and that works fine, but of course it won't suit my needs.

推荐答案

您的路由需要沿着的行{控制器} / {行动}要建立/ {与firstItem} 。如果你离开了路由作为默认的 {控制器} / {行动} / {ID} 的global.asax.cs 文件,则需要在 id来传递

Your routing needs to be set up along the lines of {controller}/{action}/{firstItem}. If you left the routing as the default {controller}/{action}/{id} in your global.asax.cs file, then you will need to pass in id.

routes.MapRoute(
    "Inventory",
    "Inventory/{action}/{firstItem}",
    new { controller = "Inventory", action = "ListAll", firstItem = "" }
);

...或某事接近。

... or something close to that.

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

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