如何从控制器中检索值以查看 [英] how to retrieve the value from controller to view

查看:58
本文介绍了如何从控制器中检索值以查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用asp.net MVC3.在我的控制器中,我有一个操作方法,例如:

I am using asp.net MVC3.In my controller I have an action method like:

public ActionResult ListView(int id)
        {
           ViewBag.key = id;
            return View();
        }



在视图中,如何在url
中动态传递此值 我通过了如下网址:
"/Home/GetDetails/id =" +"@ ViewBag.key"
我可以通过这种方式传递ID吗?
我收到类似参数字典包含用于方法system.web.MVC.ActionRestult ...的非空类型"System.Int32"的参数"id"的空条目的错误,您能告诉我吗?我可以通过其他任何方式将此ID动态传递给url.



In the view, how To pass this value dynamically in the url
I passed in the url like:
"/Home/GetDetails/id = " + ''@ViewBag.key''
can i pass the id in this way?
I am getting an error like"The parameters dictionary contains a null entry for parameter ''id'' of non-nullable type ''System.Int32'' for method system.web.MVC.ActionRestult... ",can u tell me any other way to pass this id dynamically to the url.

推荐答案

您需要将值转换为字符串.试试:
You need to convert the value to a string. Try :
@{= Convert.ToString(ViewBag.key) }



尽管最好创建一个辅助方法.

祝你好运!



Although it would be better to create a helper method.

Good luck!


这篇关于如何从控制器中检索值以查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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