1

Hi i am started working on umbraco CMS can anybody tell me how to display contents from model to controller and return data to respective view, want to do something like this :-

ContactViewModel.cs

public int id { get; set; }
public string Name { get; set; }
public string address { get; set; }
public string Email { get; set; }
public string phone { get; set; }

ContactController.cs

var result = new ContactViewModel()
             {
                 id = 1,
                 address = "ghivto",
                 Email = "nimesh@gmail.com",
                 Name = "Nimesh khatri",
                 phone = "9898989898"
             };
return View("contactsDemo",result);

how do i list above data to particular view..? i had already tried "https://www.youtube.com/watch?v=sDQwu_DzYyc" but still i didn't get...can anyone elaborate it..thanks in advance..

Peter Szekeli
  • 2,712
  • 3
  • 30
  • 44
Nimesh khatri
  • 763
  • 12
  • 29
  • I strongly recommend you read http://24days.in/umbraco/2013/creating-reusable-code-in-mvc-apps/ if you're into adding a bit more MVC to a standard Umbraco installation, you may want to tweak it a bit, but this approach is excellent – daniel Feb 16 '16 at 22:39

2 Answers2

1

SurfaceController exposes a controller like you would use in standard MVC and also gives access to the Umbraco helpers.

public MyController : SurfaceController {
   [ChildActionOnly]
   public PartialViewResult MyAction(SomeObject data)
   {
       var result = new ContactViewModel()
             {
                 id = 1,
                 address = "ghivto",
                 Email = "nimesh@gmail.com",
                 Name = "Nimesh khatri",
                 phone = "9898989898"
             };
        return View("~/Views/PartialViews/contactsDemo.cshtml",result);
   }
}

You can access this data from a Template's razor view or a Macro partial by using:

@Html.Action("MyAction","MyController", new { data = new { Test = "I am data" } })

Note: The data passing can be anything, from a single string to full object which binds to variables at the action.

As with MVC there a few ways to access a partial view file. The above example has an "absolute" path, which you could also match your view folder structure to the name of the controller and the view name to the action name. (MyAction.cshtml).

Sam Sussman
  • 1,005
  • 8
  • 27
0

If you want to do that in Umbraco, and still have access to the Umbraco tempting system etc, you need to use a SurfaceController. This is a controller that inherits all of the useful Umbraco Stuff.

Basically, you'd create your ViewModel, and then a Surface Controller to handle all the actions (Render, Process etc). You'd then call that Surface Controller on your Umbraco View for the Contact Page (or whichever page you want this to show on).

It looks like the video you linked to already covers how to do this, but you can find an example that's pretty similar to your example here: http://creativewebspecialist.co.uk/2013/07/22/umbraco-mvc-what-on-earth-is-a-surface-controller/

Tim
  • 4,217
  • 1
  • 15
  • 21