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

As sample code, above, I have a method that returns .cshtml view, called ActionMethod2.
I want to use the returned html as a string variable in my action method.How is that possible?

Elnaz
  • 2,854
  • 3
  • 29
  • 41
  • I don't want to use the result of ActionMethod2() in any view, just I want to use it as a variable value. – Elnaz Nov 12 '16 at 06:27
  • 1
    Please note that the model-view-controller tag is for questions about the pattern. There is a specific tag for the ASP.NET-MVC implementation. –  Nov 12 '16 at 06:35

3 Answers3

1

You can use Content Method for this.

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

Or you can set return type as string and pass your HTML string.

public string ActionMethod2()
{
    return "<html></html>";
}
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
  • How can I call the ActionMethod2 from MyActionMethod? Is string myVar= ActionMethod2(model).toString(); correct? – Elnaz Nov 12 '16 at 06:29
  • It does not work: string myVar= ActionMethod2(model).toString(); It returns empty string. – Elnaz Nov 12 '16 at 07:26
  • As I mentioned in question, ActionMethod2 returns a .cshtml view. I want to call the ActionMethod2() and use it's returned value as string and then pass the string to other. – Elnaz Nov 12 '16 at 07:30
  • We are just typing Simultaneously! this is why the conversation continues irrelevant. I used ActionResult (since I can't change the structure and signature of ActionMethod2() ) – Elnaz Nov 12 '16 at 07:38
0

First Mistake is ActionMethod2 return View, And you can get it directly from 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);
}

try this and it will work with you.

Ahmed
  • 1,542
  • 2
  • 13
  • 21
0

Approach One could be pass your wanted params as part of the routeValues parameter of the RedirectToAction() method.Using Query string data passed.

Or you can frame it with the help of query strings like:

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

Or You can make use of 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();
}

In the URL bar of the browser.
This solution uses a temporary 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);
}

Or try Approach 4: Just call the action no need for redirect to action or the new keyword for model.

 [HttpPost]
    public ActionResult MyActionMethod(MyModel myModel1)
    {
        return ActionMethod2(myModel1); //this will also work
    }
    public ActionResult ActionMethod2(MyModel myModel)
    {
        return View(myModel);
    }
Elnaz
  • 2,854
  • 3
  • 29
  • 41
Kay Albertus
  • 11
  • 1
  • 3
  • Welcome to stack overflow :) Of course I don't want to redirect to any action. I wanted to use the result of an action method as value of a variable in other methods. – Elnaz Nov 13 '16 at 06:25