Copied to clipboard

Flag this post as spam?

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


  • Lasse 49 posts 161 karma points
    Apr 24, 2019 @ 05:29
    Lasse
    0

    How to a partial that requires a model inside a view that is using a different model

    Hello, this is not the first time i've run into this issue, and i have never successfully solved it, so i am calling for help because i cannot figure out how to make it work as i want, the way i want.

    What i am trying to do: I have a view, that is created to handle my custom grid editor with umbraco, this view should render or call another view or partial regarding of a dropdown from umbraco.

    Say i choose 'Database1' in my dropdown, then it should render a partial with table of data, if i choose say 'Database2' (the naming is just temporary), it should render another partial view containing a table of data.

    I hope you understand my purpose, but i keep getting this error Error message


    Partial View Model My model for the partial view


    Model Controller Controller for the model


    My Partial View Partial View

    Main View Partial View Sorry for the layout of this post, i can't seem to make it look good

  • Mayur Deore 13 posts 146 karma points c-trib
    Apr 24, 2019 @ 05:59
    Mayur Deore
    0

    Hi Lasse,

    The Partial view requires model object of type

    IEnumerable<TVScreenViewerUmbraco.Dev.Models.Database1Model>
    

    And you have not passed the model in RenderPartial Method. So by default the model passed in partial view is model of main view (The view where you are calling partial view).

    You need to specify the model while calling RenderPartial method.

    var modelForPartialView=new List<TVScreenViewerUmbraco.Dev.Models.Database1Model>(); 
    //set modelForPartialView variable with data you want pass to partial view
    
    @Html.RenderPartial(partialView:"Monitor/Table1",model:modelForPartialView)
    
  • Lasse 49 posts 161 karma points
    Apr 24, 2019 @ 06:04
    Lasse
    0

    Hello Mayur, thanks for your time and fast reply.

    So far so good, i am getting the table headers into my other view now at least, but it contains no data. I am trying to populate it through my controller for the model with some test data, but i feel like it is not connected to the sequence.

    I have always been told, it is bad practice to access database directly from a view, to populate a table

  • Mayur Deore 13 posts 146 karma points c-trib
    Apr 24, 2019 @ 06:17
    Mayur Deore
    100

    Yes, it is bad practice to access database directly from view.

    I think you have another controller with action to return the data. So in this case you can use RenderAction method.

    @{
        Html.RenderAction(actionName: "actionName", controllerName: "controllerName");
    }
    

    And the action must return the partialView. See following sample controller.

     public class TestController : Controller
        {
            public ActionResult GetData()
            {
                var modelForPartialView = new List<TVScreenViewerUmbraco.Dev.Models.Database1Model>();
    
                    // To DO
                    // Logic to set modelForPartialView variable with data
    
                    return PartialView("~/Views/Monitor/Table1", modelForPartialView);
            }
        }
    
  • Lasse 49 posts 161 karma points
    Apr 24, 2019 @ 06:27
    Lasse
    0

    I am sorry for bothering you, but i keep getting new errors, this time it is

    The controller for path '' was not found or does not implement IController
    

    If i follow the controllerName, my controller opens, so it is finding something enter image description here

  • Mayur Deore 13 posts 146 karma points c-trib
    Apr 24, 2019 @ 06:43
    Mayur Deore
    0

    Do you have controller "Database1Controller" inheriting "SurfaceController"?

    Something like follow:

     public class Database1Controller : SurfaceController
        { 
            public ActionResult Index()
            {
                var modelForPartialView = new List<TVScreenViewerUmbraco.Dev.Models.Database1Model>();
    
                // To DO
                // Logic to set modelForPartialView variable with data
    
                return PartialView("~/Views/Monitor/Table1.cshtml", modelForPartialView);
            }
        }
    
  • Lasse 49 posts 161 karma points
    Apr 24, 2019 @ 06:44
    Lasse
    1

    Yes, i posted a picture in my first post called 'Model Controller' ps: dont mind the .select(s ...) i removed it as it did nothing for my purpose

    Just noticed it said 'Internal class ...' and not public. now it seems to work

  • Lasse 49 posts 161 karma points
    Apr 24, 2019 @ 06:48
    Lasse
    0

    enter image description here

Please Sign in or register to post replies

Write your reply to:

Draft