简体   繁体   English

我们可以将ViewModel分配给ASP.Net MVC中的ViewData对象吗?

[英]Can we assign a ViewModel to a ViewData object in ASP.Net MVC?

The question seems strange but I am using the DevExpress library and I need to send my own object (a viewmodel) to a controller action via a DevExpress BeginCallback event. 这个问题似乎很奇怪,但是我正在使用DevExpress库,我需要通过DevExpress BeginCallback事件将自己的对象(视图模型)发送到控制器动作。

Basically I have a simple and advance search, the simple search works prefectly as that is only a string value 基本上,我有一个简单而高级的搜索,该简单搜索非常有效,因为它只是一个字符串值

    [ValidateInput(false)]
    [Authorize]
    public ActionResult DevExpressGridView(string simple, AdvanceSearch adv)
    {
          ViewData["data"] = simple;
         ..Search Logic
    }

So when the Gridview is populated, I want to double click on a row in order to fetch the item from the database 因此,当填充Gridview时,我想双击一行以便从数据库中获取项目

    function CallBack(s, e) {

        e.customArgs['simple'] = "@ViewData["data"]";
    }

lastly the Gridview Action method 最后是Gridview Action方法

  @Html.Action("DevExpressGridView", new { simple = @ViewData["data"] })

But if the user wants to do an AdvanceSearch which is my own ViewModel, how could I send the data back via the callback? 但是,如果用户要执行一个AdvanceSearch(这是我自己的ViewModel),如何通过回调将数据发送回去? If possible at all? 如果可能的话?

    [ValidateInput(false)]
    [Authorize]
    public ActionResult DevExpressGridView(string simple, AdvanceSearch adv)
    {
          ViewData["data"] = adv;
         // Its my own type so it can't work can it?
    }

I would ask the DevExpress support team but I am still getting my license so they won't help until then 我会问DevExpress支持团队,但我仍在获取许可证,因此他们在那时将无济于事

Thanks in advance 提前致谢

ViewData is of type ViewDataDictionary which implements IDictionary<string,object> , as you can see from the relevant msdn documentation page . ViewData的类型为ViewDataDictionary ,它实现IDictionary<string,object> ,如您从相关的msdn文档页面所见。 So you can store any type of object in it, although I would personally instead prefer to return a viewmodel in most cases. 因此,您可以在其中存储任何类型的对象,尽管我个人更喜欢在大多数情况下返回视图模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM