从操作方法中调用 returns html 的方法

Call a method that returns html from an action method

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;
}

作为示例代码,上面,我有一个 returns .cshtml 视图的方法,称为 ActionMethod2.
我想在我的操作中使用返回的 html 作为字符串变量 method.How 可以吗?

您可以为此使用 Content Method

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

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

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

第一个错误是ActionMethod2return查看,而且可以直接从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);
}

试试这个,它会与你一起工作。

方法之一是将您想要的参数作为 RedirectToAction() 的 routeValues 参数的一部分进行传递 method.Using 查询字符串数据已传递。

或者您可以在查询字符串的帮助下构建它,例如:

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