在 MVC 中,为什么 RedirectToAction() 中的routeValues 属性不接受我的类作为参数?
所以,事情是这样的,我希望能够将任何可枚举的项目导出到 Excel:
这是我的应用程序的某些区域中的一个 ActionMethod,它构造了一个“ExportToExcel”模型,然后将其重定向到另一个控制器中的操作方法,另一个控制器是所有格式化为 Excel 的工作都完成了:
public ActionResult ExportCustomListToExcel()
{
var exportModel = new ExportToExcelModel();
//Here I fill up the model with a dataTable / other file info like
//exportModel.Items = blah blah..
return RedirectToAction("ExportToExcel", "Shared", new { model = exportModel, testString = "test", area = "Shared" });
}
这是我的共享 ExportToExcel ActionMethod:
public ActionResult ExportToExcel(ExportToExcelModel model, string testString)
{
//PROBLEM IS RIGHT HERE!
// where testString == "test"
// but model == null :(
//Ommited unrelated code
}
我的 ExportToExcel actionMethod 被击中,但在途中我的 ExportToExcelModel 丢失了:(
注意:它成功了在传递像“testString”这样的字符串时,我的模型有什么问题吗?
以防万一,ExportToExcelModel 是:
public class ExportToExcelModel
{
public ExportToExcelModel() {}
public ExportToExcelModel(string fileName, ItemType itemType, IEnumerable<ExportableToExcelItem> items)
{
this.FileName = fileName;
this.ItemType = ItemType;
this.Items = items;
}
public string FileName { get; set; }
public ItemType ItemType { get; set; }
public IEnumerable<ExportableToExcelItem> Items { get; set; }
}
提前感谢!
自从我遇到过其他问题以来,我第一次真正需要在这里提出问题。我发现已经在这里回答了:)
编辑:发布 FormCollection 结果:
http://imageshack.us/photo/my-images/861/sinttulonsa.png 抱歉,新手不能发图片:(
So here's the deal, i want to be able to export any Enumerable of items to excel:
Here's an ActionMethod in some Area of my app that constructs an "ExportToExcel" model, then Redirects it to an Action Method in another controller and another are which does all the formatting-to-excel work:
public ActionResult ExportCustomListToExcel()
{
var exportModel = new ExportToExcelModel();
//Here I fill up the model with a dataTable / other file info like
//exportModel.Items = blah blah..
return RedirectToAction("ExportToExcel", "Shared", new { model = exportModel, testString = "test", area = "Shared" });
}
And here's my Shared ExportToExcel ActionMethod:
public ActionResult ExportToExcel(ExportToExcelModel model, string testString)
{
//PROBLEM IS RIGHT HERE!
// where testString == "test"
// but model == null :(
//Ommited unrelated code
}
My ExportToExcel actionMethod gets hit, but somewhere along the way my ExportToExcelModel gets lost :(
Note: It succeeds on passing strings like "testString" so is there somwthing wrong with my model?
Just in case, the ExportToExcelModel is:
public class ExportToExcelModel
{
public ExportToExcelModel() {}
public ExportToExcelModel(string fileName, ItemType itemType, IEnumerable<ExportableToExcelItem> items)
{
this.FileName = fileName;
this.ItemType = ItemType;
this.Items = items;
}
public string FileName { get; set; }
public ItemType ItemType { get; set; }
public IEnumerable<ExportableToExcelItem> Items { get; set; }
}
Thanks in advance!
First time i've ever needed to actually ask a question here since every other question i've ever had i've found already answered here :)
EDIT: Posting FormCollection results:
http://imageshack.us/photo/my-images/861/sinttulonsa.png
Sorry, newbies cant post pics :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原因是 RedirectToAction 结果将启动 GET 请求 并且您的参数必须通过查询字符串传递。显然,网址可以包含的字符数有限制。
在我看来,您应该在类中而不是在另一个操作后面进行到 Excel 的转换。
因此
CustomExportAction1
和CustomExportAction2
都调用或类似的东西。
The reason is that a RedirectToAction result will launch a GET request and your parameters will have to be passed along through the querystring. Obviously there is a limit to the amount of characters a url can consist of.
Seems to me that you should do the conversion to Excel in a class instead of behind another Action.
So
CustomExportAction1
andCustomExportAction2
both callor something similar.
尝试切换您的 ExportToExcel 签名以
查看 FormCollection 中的内容(可能会有所帮助),并查看 UpdateModel 是否引发异常,因为这就是当您使操作方法采用模型而不是模型时所看到的背后发生的情况一个表单集合。
希望能帮助您找到它
更新:
您可能必须使用 TempData 来执行此操作, 阅读此内容,据说您无法使用 ASP.NET MVC 开箱即用地执行此操作!
try to switch your ExportToExcel signature to
look at what's in the FormCollection (that might help), and also see if UpdateModel is throwing an exception, because this is what is happening behind the seen when you make your action method take in a model instead of a FormCollection.
Hope that help you track it down
UPDATE:
You might have to do it using TempData, read this, supposedly you can't do this out of the box with ASP.NET MVC!!