0

I am using @Html.Action to call contoller action to return a string. I want to save the string to a variable inside razor as it is comma-seperated.

@Html.Action("GetCategories", new { SP = @ViewBag.Name, SD = "Cad" }).ToString();
//@String s = save above string in here
//@string[] arrS = s.Split(','); 

@String test = @Html.Action(......) //tried this but does NOT work.

Public ActionResult GetCategories(string SP, string SD)
{
  //Code missing
  return Content(return "sugar");
}

How can I save the Html.Action returned data to a string ?

3
  • try this. @String test = "@Html.Action(......)"; Commented Jul 29, 2014 at 14:11
  • 1
    does not work gives error: 'string' is a 'type' but is used like a 'variable' Commented Jul 29, 2014 at 14:18
  • 1
    This: @{string test = Html.Action("SomeAction").ToString();} works for me in MVC5 (sorry get test under MVC3). Commented Jul 29, 2014 at 14:39

2 Answers 2

2

This works for me, just tried in a local MVC project:

@{
      string test;
      test = @Url.Action("actionName");          
 }
Sign up to request clarification or add additional context in comments.

Comments

1

Url.Action only/always returns a Url. Not sure how this selected answer solved the problem for you unless you DID want the Url (e.g. /Controller/Action/...).

Seems Html.Action was what you wanted all along. It would return the contents "sugar" if you simply had your return coded like this;

return Content("sugar");

You can display it right on the page or use the suggested syntax from "Mark" to save it to a varible

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.