MVC3 将具有 Iqueryable 的视图模型对象传递回控制器
我有一个视图,我将视图模型对象传递给该视图,其中包含 IQueryable<>目的。
该对象用于填充 mvccontrib 网格。该视图还包含其他部分视图,允许用户过滤网格内的数据。
一旦网格被过滤,我希望用户能够将 Iqueryable 对象导出到另一个控制器 actionresult 方法,然后调用另一个将数据导出到 Excel 的视图模型。
以下是调用 Export actionresult() 方法的视图片段:
@using (Html.BeginForm("Export", "Home", FormMethod.Post, new { Model }))
{
<p>
<input class="button" value="Export to Excel" type="submit" />
</p>
}
模型确实包含 IQueryable 对象。
当我调试代码时,我可以查看视图模型对象,当然,为了填充 IQueryable,我必须枚举该对象。
我还创建了另一个 viewmodel 对象,一旦 Model 对象传递回 actionresult 方法,就会尝试使用 .ToList() 方法或 AsEnumerable() 方法枚举 IQueryable 对象。
但在所有情况下,IQueryable 对象都会作为空对象传递给控制器。
以下是从视图中调用的操作结果方法:
[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");
}
任何帮助将不胜感激。
谢谢
乔
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() method.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能像这样传递复杂的对象:
new { Model }
。这本来就很容易:-)。您必须将它们一一发送:显然这可能会非常痛苦。所以我建议您只发送一个 id:
然后在应该导出到 Excel 的控制器操作中使用此 id 从您最初在 GET 操作中获取对象的位置(大概是数据库或其他东西)获取对象。如果您想保留分页以及用户可以在网格上执行的操作,您也可以将它们发送到服务器:
另一种可能性(我不推荐)包括将此对象保存到会话中,以便您可以稍后取回。
另一种可能性(我仍然不推荐)是使用 MVCContrib 的 Html.Serialize 帮助器,它允许您将整个对象图序列化到隐藏字段中,并且在提交表单时将其发送到服务器,您将能够将其作为操作参数获取。
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:Obviously this could get quite painful. So what I would recommend you is to send only an 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:
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.
简单的答案是:不要将 IQueryable 属性放入模型中。该模型应该是纯粹的简单对象和验证属性。将可查询性保留在控制器中。
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.