C# 通过调用方法从方法返回视图
我在名为AspNetAssessment\u QuestionController的控制器中有一个方法C# 通过调用方法从方法返回视图,c#,asp.net,asp.net-mvc,razor,C#,Asp.net,Asp.net Mvc,Razor,我在名为AspNetAssessment\u QuestionController的控制器中有一个方法 public ActionResult Excel_Data(HttpPostedFileBase excelfile) { if ( excelfile == null ) { ViewBag.Error = "Please select an excel file"; return View("Create"); } else
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数据来运行其razor语法,如在“Create”和“Index”中,它们都有:
@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_数据方法返回视图时,我收到一个错误,即以下url中缺少Viewbag.propertyname数据
http://localhost:1331/AspNetAssessment_Question/Excel_Data
我甚至尝试调用他们的方法,比如Create()而不是returnview(“Create”),但失败了,没有呈现视图
create方法有两种形式。一个点击Excel_数据的方法,另一个调用Create方法
如何点击他们的方法并从Excel_数据返回视图,以便他们从他们的方法以及Excel_数据中获取viewbag数据
excel_data method assign viewbag.Error,在create的方法中,我有viewbag.ClassID和更多信息。如果您只想显示错误消息,您可以将它们保存到session/cookie中,并使用RedirectToAction
返回RedirectToAction(“create”,“AspNetAssessment\u QuestionController”)代码>而不是返回视图(“创建”)代码>
然后在创建/索引ActionResults中,您可以添加如下内容:
public ActionResult Create()
{
ViewBag.Error = Session["ErrorMessage"].ToString();
return View();
}
您的问题的解决方案非常简单
在给出解决方案之前,我们需要了解view和redirecttoaction两件事及其区别:
简言之,return view()类似于asp.net中的server.Transfer(),而redirecttoaction将命中对浏览器的302请求
详细信息可以在这篇非常好的文章中找到:
因此,现在您的问题的解决方案是:
用于返回redirectoaction()而不是返回视图()
您的示例稍作修改:
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
{
TempData["Error"] = "File type is incorrect"; //replaced tempdata with viewbag
return RedirectToAction("Create","AspNetAssessment_QuestionController")
}
}
@if(tempdata["error"]!= null)
{
var result = tempdata["error"];
}
注意:要保留错误消息,请使用tempdata[“error”]而不是viewbag.error,如上述代码所述
以及tempdata的数据,您可以在下面的create视图中直接访问,而无需使用create action方法执行任何操作
创建视图代码示例:
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
{
TempData["Error"] = "File type is incorrect"; //replaced tempdata with viewbag
return RedirectToAction("Create","AspNetAssessment_QuestionController")
}
}
@if(tempdata["error"]!= null)
{
var result = tempdata["error"];
}
就这样
供参考:
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
{
TempData["Error"] = "File type is incorrect"; //replaced tempdata with viewbag
return RedirectToAction("Create","AspNetAssessment_QuestionController")
}
}
@if(tempdata["error"]!= null)
{
var result = tempdata["error"];
}
如果您需要其他信息,如如何强制转换不同的tempdata对象以及viewbag、viewdata、tempdata之间的差异,请参考以下链接:
那篇文章有很好的解释
希望这会有帮助
谢谢
Karthik您是否考虑过使用partialView?我赞成在会话/cookie中使用tempdata
,我在回答中使用了它。干得好,先生!谢谢,先生。很高兴分享