【问题标题】:Call a method that returns html from an action method调用从动作方法返回 html 的方法
【发布时间】:2016-11-12 06:21:39
【问题描述】:
public ActionResult MyActionMethod(MyModel model)
{
    //some code
    string myVar= ActionMethod2(model).toString();
    //use myVar
    Method3(myVar, otherVar);
}

public ActionResult ActionMethod2()(MyModel model)
{
    return View(model);
}

private  Method3(string myVar,  int otherVar)
{
    //do something;
}

作为上面的示例代码,我有一个返回.cshtml 视图的方法,称为ActionMethod2
我想在我的操作方法中使用返回的 html 作为字符串变量。这怎么可能?

【问题讨论】:

  • 我不想在任何视图中使用 ActionMethod2() 的结果,只是想将其用作变量值。
  • 请注意,model-view-controller 标签是针对有关模式的问题。 ASP.NET-MVC 实现有一个特定的标记。

标签: html asp.net-mvc controller renderaction


【解决方案1】:

您可以为此使用Content Method

public ActionResult ActionMethod2()
{
    return Content("YourHTMLString");
}

或者您可以将返回类型设置为字符串并传递您的 HTML 字符串

public string ActionMethod2()
{
    return "<html></html>";
}

【讨论】:

  • 如何从 MyActionMethod 调用 ActionMethod2?是字符串 myVar=ActionMethod2(model).toString();对吗?
  • 它不起作用:string myVar= ActionMethod2(model).toString();它返回空字符串。
  • 正如我在问题中提到的,ActionMethod2 返回一个 .cshtml 视图。我想调用 ActionMethod2() 并将它的返回值用作字符串,然后将字符串传递给其他。
  • 我们只是同时打字!这就是为什么谈话继续无关紧要的原因。我使用了 ActionResult (因为我无法更改 ActionMethod2() 的结构和签名)
【解决方案2】:

第一个错误是ActionMethod2return View,可以直接从MyActionMethod获取

protected string RenderPartialViewToString(string viewName, object model)
    {
        if (string.IsNullOrEmpty(viewName))
            viewName = ControllerContext.RouteData.GetRequiredString("action");

        ViewData.Model = model;

        using (var sw = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
            var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw);

            return sw.GetStringBuilder().ToString();
        }
    }


public ActionResult MyActionMethod(MyModel model)
{
     //some code
     //string myVar= ActionMethod2(model).toString();
     var myVar = RenderPartialViewToString("yourviewName", model);
     //use myVar
     Method3(myVar, otherVar);
}

试试这个,它会对你有用。

【讨论】:

    【解决方案3】:

    方法一可以将您想要的参数作为 RedirectToAction() 方法的 routeValues 参数的一部分传递。使用传递的查询字符串数据。

    或者您可以借助以下查询字符串对其进行框架化:

    return RedirectToAction( "Main", new RouteValueDictionary( 
        new { controller = controllerName, action = "YourActionName", Id = Id}) );
    

    或者您可以使用 TempData:

    [HttpPost]
    public ActionResult MyActionMethod(MyModel model)
    {
        TempData["myModal"]= new MyModel();
        return RedirectToAction("ActionMethod2");
    }
    
    [HttpGet]
    public ActionResult ActionMethod2()
    {
        MyModel myModal=(MyModel)TempData["myModal"];
        return View();
    }
    

    在浏览器的 URL 栏中。
    此解决方案使用临时 cookie:

    [HttpPost]
    public ActionResult Settings(SettingsViewModel view)
    {
        if (ModelState.IsValid)
        {
            //save
            Response.SetCookie(new HttpCookie("SettingsSaveSuccess", ""));
            return RedirectToAction("Settings");
        }
        else
        {
            return View(view);
        }     
    }
    
    [HttpGet]
    public ActionResult Settings()
    {
        var view = new SettingsViewModel();
        //fetch from db and do your mapping
        bool saveSuccess = false;
        if (Request.Cookies["SettingsSaveSuccess"] != null)
        {
            Response.SetCookie(new HttpCookie("SettingsSaveSuccess", "") { Expires = DateTime.Now.AddDays(-1) });
            saveSuccess = true;
        }
        view.SaveSuccess = saveSuccess;
        return View(view);
    }
    

    或尝试方法 4: 只需调用操作,无需重定向到操作或模型的新关键字。

     [HttpPost]
        public ActionResult MyActionMethod(MyModel myModel1)
        {
            return ActionMethod2(myModel1); //this will also work
        }
        public ActionResult ActionMethod2(MyModel myModel)
        {
            return View(myModel);
        }
    

    【讨论】:

    • 欢迎来到堆栈溢出 :) 当然我不想重定向到任何动作。我想将一个动作方法的结果用作其他方法中的变量值。
    猜你喜欢
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多