我有一個名爲AspNetAssessment_QuestionController通過調用它的方法返回從方法的觀點
public ActionResult Excel_Data(HttpPostedFileBase excelfile)
{
if (excelfile == null)
{
ViewBag.Error = "Please select an excel file";
return View("Create");
}
else if (excelfile.FileName.EndsWith("xls") || excelfile.FileName.EndsWith("xlsx"))
{
return View("Index");
}
else
{
ViewBag.Error = "File type is incorrect";
return View("Create");
}
}
控制器的方法現在,當這個方法返回的視圖中,請求的視圖需要一些viewbag數據來運行其剃刀語法,就像在「創建」和「索引」都有:
@Html.DropDownList("ClassID", null, htmlAttributes: new { @class = "form-control" })
由於該系統只返回我的看法沒有擊中它不能從創建和索引的方法獲取viewbag的值的方法。
我還添加了路由,以便他們打他們的方法routes.config文件
routes.MapRoute(
name: "AspNetAssessment_Question/Create",
url: "{controller}/{action}/{id}",
defaults: new { controller = "AspNetAssessment_Question", action = "Create", id = UrlParameter.Optional }
);
,並同樣適用於指數,但是當excel_data方法返回查看我收到一個錯誤Viewbag.propertyname數據丟失的以下網址
http://localhost:1331/AspNetAssessment_Question/Excel_Data
我甚至試着撥打自己的方法,如創建(),而不是返回視圖(「創建」),但它失敗了,並沒有渲染視圖。
創建方法有兩種形式。一個命中Excel_data和另一個調用創建方法的方法創建方法
我該如何擊中他們的方法並從Excel_Data中返回視圖,以便他們從Excel_Data中獲取viewbag數據以及它們的方法和Excel_Data。
excel_data方法分配viewbag.Error和在創建的方法我有viewbag.ClassID和更多。
你有沒有考慮使用partialviews的? – Gino