繁体   English   中英

通过调用方法从方法返回视图

[英]Returning a view from a method by calling its method

我在名为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" })

由于系统仅返回我的视图而没有点击该方法,因此无法从Create and Index方法获取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方法返回视图时,我收到以下URL上缺少Viewbag.propertyname数据的错误

http://localhost:1331/AspNetAssessment_Question/Excel_Data 

我什至尝试调用他们的方法,例如Create()而不是返回view(“ Create”),但失败了,并且没有呈现视图。

create方法有两种形式。 一个命中Excel_data方法,另一个命中Create方法

我如何才能找到他们的方法并从Excel_Data返回视图,以便他们从他们的方法以及从Excel_Data获取视图包数据。

excel_data方法分配viewbag.Error,在创建方法中,我具有viewbag.ClassID等。

解决问题的方法非常简单

在给出解决方案之前,我们需要了解view和redirecttoaction及其区别的两点:

简而言之,view()就像asp.net中的server.Transfer(),而redirecttoaction会向浏览器发送302请求。

详细信息可以在这篇很好的文章中找到: http : //www.dotnettricks.com/learn/mvc/return-view-vs-return-redirecttoaction-vs-return-redirect-vs-return-redirecttoroute

所以现在解决您的问题的方法是:

用于返回redirectoaction()而不是return view()

您的示例稍作修改:

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")
            }

        }

注意:要保留错误消息,请使用tempdata [“ error”]代替上面代码中提到的viewbag.error

而且,您可以像下面这样在create视图中直接访问tempdata的数据,而无需执行create action方法。

创建视图代码,例如:

@if(tempdata["error"]!= null)
{
    var result = tempdata["error"];
}

而已

仅供参考:

如果您需要其他信息,例如如何转换不同的tempdata对象以及viewbag,viewdata和tempdata之间的区别,请参考以下链接: http : //www.binaryintellect.net/articles/36941654-8bd4-4535-9226-ddf47841892f .aspx

那篇文章有很好的解释

希望对您有所帮助

感谢Karthik

如果仅是要显示的错误消息,则可以将它们保存到会话/ cookie中,并使用RedirectToAction return RedirectToAction("Create", "AspNetAssessment_QuestionController"); 而不是return View("Create");

然后,在“创建/索引ActionResults”中,可以添加以下内容:

public ActionResult Create()
{
    ViewBag.Error = Session["ErrorMessage"].ToString();
    return View();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM