Asp.net mvc '中的服务器错误/';ActionLink中的应用程序(但提交效果良好)
执行以下行Asp.net mvc '中的服务器错误/';ActionLink中的应用程序(但提交效果良好),asp.net-mvc,asp.net-mvc-4,razor,Asp.net Mvc,Asp.net Mvc 4,Razor,执行以下行 @Html.ActionLink("Open",actionName: "DownloadExcelFile", controllerName: "Excel", routeValues: new { model = new ExcelModel(5, "JobName", "item.UserName") }, htmlAttribut
@Html.ActionLink("Open",actionName: "DownloadExcelFile",
controllerName: "Excel",
routeValues: new { model = new ExcelModel(5, "JobName", "item.UserName") },
htmlAttributes: null)
返回“/”应用程序中的服务器错误,您能帮我修复它们吗
请注意,当我更改参数的名称时,model->id,我会在“/”应用程序中得到一个错误404,而不是服务器错误
模型是
public class ExcelModel
{
public int InputA { get; set; }
public string InputB { get; set; }
public string UserName { get; set; }
public ExcelModel(int inputA, string inputB, string userName)
{
InputA = inputA;
InputB = inputB;
UserName = userName;
}
public ExcelModel()
{
InputA = 1;
InputB = "B1";
UserName = "NiceUser";
}
...
}
public class ExcelController : Controller
{
public ActionResult Index()
{
var model = new ExcelModel(1, "B1", User.Identity.Name);
return View(model);
}
[HttpPost]
public ActionResult DownloadExcelFile(ExcelModel id)
{
// assume we create an an excel stream...
MemoryStream excelStream = id.BuildSpreadsheet();
return new FileStreamResult(excelStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = "myfile.xslx"
};
}
}
控制器是
public class ExcelModel
{
public int InputA { get; set; }
public string InputB { get; set; }
public string UserName { get; set; }
public ExcelModel(int inputA, string inputB, string userName)
{
InputA = inputA;
InputB = inputB;
UserName = userName;
}
public ExcelModel()
{
InputA = 1;
InputB = "B1";
UserName = "NiceUser";
}
...
}
public class ExcelController : Controller
{
public ActionResult Index()
{
var model = new ExcelModel(1, "B1", User.Identity.Name);
return View(model);
}
[HttpPost]
public ActionResult DownloadExcelFile(ExcelModel id)
{
// assume we create an an excel stream...
MemoryStream excelStream = id.BuildSpreadsheet();
return new FileStreamResult(excelStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = "myfile.xslx"
};
}
}
RouteConfig是标准配置
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
最后,如前所述,该方法本身很好,因为它与submit完美配合,如下所示:
@using (Html.BeginForm("DownloadExcelFile", "Excel"))
{
<fieldset>
// fields names and values
<p>
<input type="submit" value="Open Excel"/>
</p>
</fieldset>
}
@使用(Html.BeginForm(“下载Excel文件”、“Excel”))
{
//字段名称和值
}
您的控制器方法用属性标记。这意味着它只接受POST请求,而不接受GET请求。正常的链接访问是GET请求,所以这可能就是问题所在。()
删除HttpPost
属性,看看这是否解决了问题。1)不能将整个类作为路由值参数传递。助手必须能够将您传递的任何内容放入URL,这意味着它必须是可以转换为字符串值的内容。可以对模型进行JSON编码,然后将JSON字符串传递给param,但是助手不会为您做出这样的假设,也不一定知道如何为您进行JSON编码,如果它这样做的话
2) 当您只传递id时,您会得到一个404,因为您的操作不接受
id
的int,而是期望ExcelModel
,正如我们在#1中所讨论的,这是不可能通过URL传递的。您是对的,我很困惑,因为我能够传递类的一个blanc实例,如中所示