1 Replies - 6047 Views - Last Post: 29 July 2013 - 04:43 PM

#1 istore221  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 136
  • Joined: 11-December 11

How to call a Action method from view

Posted 28 June 2013 - 01:40 AM

I have a problem. i cant figure it out. i want to call a action in my User controller it return a string and returned value use from the view

my action - controller is User

public string getUserFullName()
        {
            return "Gayani Perera";

        }


how do i call this action and get the returned value and use it on view.im using razor
and no ajax need . i want to call this method on page load . how can i do it

so far my view

<h4 align="center">Hello @{string Name = "call action from here"; @Html.Raw(Name);}</h4>


Is This A Good Question/Topic? 0
  • +

Replies To: How to call a Action method from view

#2 BattlFrog  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 72
  • Joined: 09-April 12

Re: How to call a Action method from view

Posted 29 July 2013 - 04:43 PM

Are you going TO the view, or FROM the view. If you are returning the string TO the view from the controller, just dump it in a model and return the model. So:

Model:

public class MyModel
{
  public string Name { get; set; }
}



Controller:

public ActionResult Index()
{
  MyModel model = new MyModel { name = "Mary Poppins" };

  return View(model);

}



View:

@model MyModel

@Html.LabelFor(m => m.Name)




That should do it.

This post has been edited by BattlFrog: 29 July 2013 - 04:44 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1