如何将委托对象传递给MVC Views。

时间:2013-05-02 12:17:27

标签: asp.net-mvc delegates

我的控制器中有一个委托我希望将其传递给我的视图以传递到我的设置中。

我已经尝试将委托分配给ViewData [“delegateFunction”] = delegateFunction;

但这也不是有效的想法..

我知道这是一个基本问题。但只是了解代表。

提前致谢, saravanakumar。

1 个答案:

答案 0 :(得分:0)

我使用Model类将委托传递给View:

模型

public class ExampleModel
{
    public Func<string, string> getUIName { get; set; }
}

方法声明

public string concreteMethod_GetUIName(string text)
{
    return text;
}

控制器

public ActionResult Index()
{
    ExampleModel model = new ExampleModel { getUIName = concreteMethod_GetUIName };

    return View(model);
}

查看

@model ExampleModel

@Model.getUIName("sample text")