I'm new to MVC too, so I'm not sure of an exact answer, but hopefully the following will help.
Regarding your questions:
The Surface Controller is in charge of making the query and passing
off the list of results to the View.
The ViewModel turns the table data into an Object with similar
properties that can be seen by the View.
The View/Partial displays the input and output fields the user will
interact with (textboxes, buttons, clickable things).
You're close, but I would put it more like this:
The Surface Controller is in charge of making the query and passing
off the list of results to the View.
The ViewModel defines the structure of an object that will store the table data, as properties that can be seen by the View. It doesn't convert anything by itself, the controller inserts data into instances of it.
The View/Partial displays the input and output fields the user will
interact with (textboxes, buttons, clickable things).
Are you interacting with any Umbraco data in this controller? If so, you'll want to query your data using the Umbraco APIs instead, and use the models already generated by Models Builder. If not, you may possibly want to use a regular MVC controller instead of an Umbraco Surface Controller, but I'm not sure if it makes a difference.
I'd suggest reading at least up to "Accessing Your Model's Data from a Controller" on Microsoft's MVC 5 documentation here. Where it really gets to what you want is under the heading "Strongly Typed Models and the @model Keyword".
Anyway, I'm pretty sure you want your getPayors() method to return an object of type ActionResult, and then query the database inside it, and pass that result into your view as part of the return statement.
Where I start to get lost is whether you'd want to keep following the Microsoft example and use Entity Framework to create a DbSet object and pass an instance of that to your view, or do it a different way.
Anyway, hopefully this will help a bit, and hopefully someone will chime in with a better answer.
Wow thank you for the thought out and detailed response! I've got a little bit of reading and testing to do now, and I'll message back if I make any developments. I really appreciate you taking the time to help me out.
using the surface controller to query your data in the DB is correct.
I would still using ActionResult as the return type.
The result would be PartialView("view", model) with that you can do @model IEnumerable<> in the partial view and then use @Model in your view to access your data.
You have to call the method of your controller in your view. Have a look at @Html.Action(). Not in the view the action is returning, int he main view.
using System.Linq;
using System.Web.Mvc;
namespace MyProject
{
public class AnviPayorListController : Umbraco.Web.Mvc.SurfaceController
{
public ActionResult GetPayors(string queryString)
{
//Conect to custom table
var db = DatabaseContext.Database;
//returns a List<AnviPayorListViewModel>
var data = db.Fetch<AnviPayorListViewModel>("select * from AnviPayor where id = @id", queryString); // change table name in select statement
//Create a view called 'GetPayors' and pass the data (Model) to it
return View(data);
}
}
public class AnviPayorListViewModel
{
public string PayorID { get; set; }
public string PayorName {get; set; }
}
}
create a View called GetPayors.cshtml in your 'Views' folder (note the model type matches the 'data' variable in the controller...)
Thank you all so much. I've definitely learned a lot more about MVC with y'alls help. I'm not all the way there on my effort, but for this thread I think my questions have been answered. Louis, your code was a blessing of a starting point. I'm going to mark this one as solved.
How do you render data queried by a Surface Controller?
I'm trying to make a search form that will fetch from a table anything that contains the query.
Currently I just want to make the search form interactive where the submit button pulls up the entire table from the database (a name and ID).
I've made a Surface Controller, a ViewModel, and a View/Partial/...
I'm new to MVC so for my understanding:
The Surface Controller is in charge of making the query and passing off the list of results to the View.
The ViewModel turns the table data into an Object with similar properties that can be seen by the View.
The View/Partial displays the input and output fields the user will interact with (textboxes, buttons, clickable things).
Is that the right idea?
How do you go from setting up the IEnumerable to outputting the data in a table?
Do I actually call my getPayors() method somewhere?
Because in the examples I've seen around the forum, there was either an ActionResult there or the IEnumerable< > method that wasn't called.
I'm new to MVC too, so I'm not sure of an exact answer, but hopefully the following will help.
Regarding your questions:
The Surface Controller is in charge of making the query and passing off the list of results to the View.
The ViewModel turns the table data into an Object with similar properties that can be seen by the View.
The View/Partial displays the input and output fields the user will interact with (textboxes, buttons, clickable things).
You're close, but I would put it more like this:
The Surface Controller is in charge of making the query and passing off the list of results to the View.
The ViewModel defines the structure of an object that will store the table data, as properties that can be seen by the View. It doesn't convert anything by itself, the controller inserts data into instances of it.
The View/Partial displays the input and output fields the user will interact with (textboxes, buttons, clickable things).
Are you interacting with any Umbraco data in this controller? If so, you'll want to query your data using the Umbraco APIs instead, and use the models already generated by Models Builder. If not, you may possibly want to use a regular MVC controller instead of an Umbraco Surface Controller, but I'm not sure if it makes a difference.
I'd suggest reading at least up to "Accessing Your Model's Data from a Controller" on Microsoft's MVC 5 documentation here. Where it really gets to what you want is under the heading "Strongly Typed Models and the @model Keyword".
Anyway, I'm pretty sure you want your getPayors() method to return an object of type ActionResult, and then query the database inside it, and pass that result into your view as part of the return statement.
Where I start to get lost is whether you'd want to keep following the Microsoft example and use Entity Framework to create a DbSet object and pass an instance of that to your view, or do it a different way.
Anyway, hopefully this will help a bit, and hopefully someone will chime in with a better answer.
Regards,
David
Wow thank you for the thought out and detailed response! I've got a little bit of reading and testing to do now, and I'll message back if I make any developments. I really appreciate you taking the time to help me out.
Hi Kevin,
using the surface controller to query your data in the DB is correct. I would still using ActionResult as the return type. The result would be PartialView("view", model) with that you can do @model IEnumerable<> in the partial view and then use @Model in your view to access your data. You have to call the method of your controller in your view. Have a look at @Html.Action(). Not in the view the action is returning, int he main view.
Regards David
Hi Kevin,
Try something along these lines (untested)
Create your Controller like so...
create a View called GetPayors.cshtml in your 'Views' folder (note the model type matches the 'data' variable in the controller...)
This is the basics of the structure you need, so tweek it as you need.
Louis
Thank you all so much. I've definitely learned a lot more about MVC with y'alls help. I'm not all the way there on my effort, but for this thread I think my questions have been answered. Louis, your code was a blessing of a starting point. I'm going to mark this one as solved.
is working on a reply...