简体   繁体   中英

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.

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

    function CallBack(s, e) {

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

lastly the Gridview Action method

  @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? 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

Thanks in advance

ViewData is of type ViewDataDictionary which implements IDictionary<string,object> , as you can see from the relevant msdn documentation page . So you can store any type of object in it, although I would personally instead prefer to return a viewmodel in most cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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