Reputation: 2890
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?
Upvotes: -1
Views: 2410
Reputation: 121
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);
}
Upvotes: 0
Reputation: 1590
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.
Upvotes: 0
Reputation: 7866
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>";
}
Upvotes: 1