ASP.NET MVC - Execute controller action without redirecting

前端 未结 3 450
遥遥无期
遥遥无期 2021-01-11 15:31

I am trying to execute an action on a controller without redirecting to the associated view for that action. For a good example of what I am trying to achieve take a look at

3条回答
  •  梦谈多话
    2021-01-11 16:10

    • using System.Web.Mvc;
    • using System.Web.Mvc.Html;

      public ActionResult Index()
      {
          HtmlHelper helper = new HtmlHelper(new ViewContext(ControllerContext, new WebFormView(ControllerContext, "Index"), new ViewDataDictionary(), new TempDataDictionary(), new System.IO.StringWriter()), new ViewPage());
          helper.RenderAction("Index2");
      
          return View();
      }
      
      public ActionResult Index2(/*your arg*/)
      {
          //your code
          return new EmptyResult();
      }
      

提交回复
热议问题