开发者

MVC3 Pass viewmodel object with Iqueryable back to Controller

开发者 https://www.devze.com 2023-04-06 17:21 出处:网络
I have a view that I pass a viewmodel object to that contains an IQueryable<> object. This object is used to populate an mvccontrib grid.The view also contains other partial views that allow the u

I have a view that I pass a viewmodel object to that contains an IQueryable<> object.

This object is used to populate an mvccontrib grid. The view also contains other partial views that allow the user to filter the data within the grid.

Once the grid is filtered I would like the user to be able to export the Iqueryable object to another controller actionresult method which then calls another viewmodel that exports the data to Excel.

Here is the snippet of the view that calls the Export actionresult() method:

@using (Html.BeginForm("Export", "Home", FormMethod.Post, new { Model }))
{
  <p>
   <input class="button" value="Export to Excel" type="submit" />
  </p>

}

Model does contain the IQueryable object.

When I debug the code I can view the viewmodel object, and of course in order to populate the IQueryable I must enumerate the object.

I have also created another viewmodel object that, once the Model object is passed back to the actionresult method attempts to enumerate the IQueryable object by either using the .ToList() method or the AsEnumerable() 开发者_如何学Pythonmethod.

But in all cases the IQueryable object is pass to the controller as a null object.

Here is the action result method that is being called from the view:

[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Export(PagedViewModel<NPSProcessed> NPSData)
{
    string message = "";
    NPSData Query = new Models.NPSData(NPSData);

    Query.IData = NPSData.Query.ToList();

        // Opening the Excel template...
        FileStream fs =
        new FileStream(Server.MapPath(@"\Content\NPSProcessedTemplate.xls"),         FileMode.Open, FileAccess.Read);

        MemoryStream ms = new MemoryStream();

        ee.ExportData(fs, ms, Query.IData, message);

        // Sending the server processed data back to the user computer...
        return File(ms.ToArray(), "application/vnd.ms-excel", "NPSProcessedNewFile.xls"); 

    }

Any assistance would be greatly appreciated.

Thanks

Joe


You cannot pass complex objects around like this: new { Model }. It would have been to easy :-). You will have to send them one by one:

new { prop1 = Model.Prop1, prop2 = Model.Prop2, ... }

Obviously this could get quite painful. So what I would recommend you is to send only an id:

new { id = Model.id }

and then inside your controller action that is supposed to export to Excel use this id to fetch the object from wherever you fetched it initially in the GET action (presumably a database or something). If you want to preserve the paging, and stuff that the user could have performed on the grid, you could send them as well to the server:

new { id = Model.id, page = Model.CurrentPage, sortColumn = Model.SortBy }

Another possibility (which I don't recommend) consists into saving this object into the session so that you can fetch it back later.

Yet another possibility (which I still don't recommend) is to use the MVCContrib's Html.Serialize helper which allows you to serialize an entire object graph into a hidden field and it will be sent to the server when the form is submitted and you will be able to fetch it as action argument.


The simple answer is: don't put IQueryable properties in your model. The model should be purely simple objects and validation attributes. Keep the queryability in your controller.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号