2016-11-12 86 views
-1
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; 
} 

作为样本代码返回HTML的方法,上述的,我有一个返回.cshtml视图,称为ActionMethod2的方法。
我想在我的操作方法中使用返回的html作为字符串变量。这是可能的吗?呼叫从一个操作方法

+0

我不想在任何视图中使用ActionMethod2()的结果,只是我想用它作为变量值。 – Elnaz

+1

请注意,模型 - 视图 - 控制器标签是针对有关该模式的问题。 ASP.NET-MVC实现有一个特定的标签。 –

回答

0

第一个错误是ActionMethod2返回查看,并可以直接从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); 
} 

让它试试这个,它会与您合作。

0

您可以使用Content Method

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

或者您可以设置返回类型为字符串,并通过您HTML字符串

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

如何从MyActionMethod调用ActionMethod2?是字符串myVar = ActionMethod2(model).toString();正确? – Elnaz

+0

它不起作用:string myVar = ActionMethod2(model).toString();它返回空字符串。 – Elnaz

+0

正如我提到的问题,ActionMethod2返回一个.cshtml视图。我想调用ActionMethod2()并将它的返回值作为字符串使用,然后将字符串传递给其他字符串。 – Elnaz

0

方法一可以传递您想要的参数作为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(); 
} 

在浏览器的地址栏。
该解决方案使用临时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); 
    } 
+0

欢迎使用堆栈溢出:)当然,我不想重定向到任何操作。我想在其他方法中使用操作方法的结果作为变量的值。 – Elnaz