user3428422
user3428422

Reputation: 4560

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

Upvotes: 0

Views: 1052

Answers (1)

elolos
elolos

Reputation: 4450

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.

Upvotes: 1

Related Questions