Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kevin 18 posts 108 karma points
    Aug 03, 2018 @ 20:31
    Kevin
    0

    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. SurfaceController

    The ViewModel turns the table data into an Object with similar properties that can be seen by the View. ViewModel

    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.

  • David Zweben 268 posts 753 karma points
    Aug 04, 2018 @ 17:20
    David Zweben
    1

    I'm new to MVC too, so I'm not sure of an exact answer, but hopefully the following will help.

    Regarding your questions:

    1. The Surface Controller is in charge of making the query and passing off the list of results to the View.

    2. The ViewModel turns the table data into an Object with similar properties that can be seen by the View.

    3. 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:

    1. The Surface Controller is in charge of making the query and passing off the list of results to the View.

    2. 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.

    3. 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

  • Kevin 18 posts 108 karma points
    Aug 06, 2018 @ 15:43
    Kevin
    0

    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.

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Aug 04, 2018 @ 19:36
    David Brendel
    1

    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

  • Louis Ferreira 69 posts 265 karma points
    Aug 07, 2018 @ 07:58
    Louis Ferreira
    100

    Hi Kevin,

    Try something along these lines (untested)

    Create your Controller like so...

    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...)

    @using MyProject
    @using NHibernate.Linq
    @model IEnumerable<MyProject.AnviPayorListViewModel>
    
    @foreach (AnviPayorListViewModel item in Model)
    {
        <p>@item.PayorName</p>
    }
    

    This is the basics of the structure you need, so tweek it as you need.

    Louis

  • Kevin 18 posts 108 karma points
    Aug 07, 2018 @ 20:35
    Kevin
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft