Copied to clipboard

Flag this post as spam?

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


  • athul 26 posts 117 karma points
    Nov 09, 2016 @ 06:02
    athul
    0

    How to display content from a controller action in partial view

    I have created a controller action method like this

        public class RegistrationFormSurfaceController : SurfaceController
    {
        [HttpGet]
        public ActionResult MemberProfile(RegistrationFormViewModel model)
        {
            //Get registration data from Database
            // fill "model" with the data
            return View(model);
        }
    }
    

    and my partial view is like this

         @model USN.Models.RegistrationFormViewModel
    <p>@Model.Address</p>
    

    And on my template I am calling this view like this

        @Html.Partial("USNAdvancedPageComponents/TDN_MemberProfile",new USN.Models.RegistrationFormViewModel())
    

    this doesn't throws any error neither shows the data. I know this is not the right approach make this work. Can anyone please point out what all I am missing? So that I can send data from a controller and display it in a partial view.

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Nov 09, 2016 @ 06:45
    David Brendel
    1

    Hi Athul,

    the Html.Partial helper just shows the partial view with the model your passing. In your case thats an empty model.

    For going to the controller and fetch your data from the db you have to use

    Html.Action("MemberProfile","RegistrationFormSurface")
    

    this calls the controller and returns your partial view with your data.

    Regards David

  • athul 26 posts 117 karma points
    Nov 09, 2016 @ 10:38
    athul
    0

    Now it throws an error

    No route in the route table matches the supplied values.

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Nov 09, 2016 @ 12:22
    Dennis Adolfi
    1

    Hi athul.

    Try the following:

    Your controller:

    public class RegistrationFormSurfaceController : SurfaceController
    {
        public ActionResult MemberProfile()
        {
            var model = new RegistrationFormViewModel();    
            //Get registration data from Database fill "model" with the data
    
            model.Address = "This is a test";    
            return View("USNAdvancedPageComponents/TDN_MemberProfile", model);
        }
    }
    

    Your partial:

    @model USN.Models.RegistrationFormViewModel
    <p>@Model.Address</p>
    

    And your template:

    @Html.Action("MemberProfile","RegistrationFormSurface")
    

    Remember that you need to build your solution (if you haveĀ“nt got all your files in App_Code) in order for Umbraco to pick up your routes.

    Hope this helps!

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Nov 09, 2016 @ 12:25
    David Brendel
    2

    Hi athul,

    as Dennis stated the Problem was that your controller method has a parameter which is not send with the action method. Make the changes he suggest and it should work.

    Regards David

  • athul 26 posts 117 karma points
    Nov 10, 2016 @ 03:01
    athul
    1

    Hi All, Now it throws an error

    The view 'USNAdvancedPageComponents/TDN_MemberProfile' or its master was not found or no view engine supports the searched locations. The following locations were searched:
    ~/Views/RegistrationFormSurface/USNAdvancedPageComponents/TDN_MemberProfile.aspx
    ~/Views/RegistrationFormSurface/USNAdvancedPageComponents/TDN_MemberProfile.ascx
    ~/Views/Shared/USNAdvancedPageComponents/TDN_MemberProfile.aspx
    ~/Views/Shared/USNAdvancedPageComponents/TDN_MemberProfile.ascx
    ~/Views/RegistrationFormSurface/USNAdvancedPageComponents/TDN_MemberProfile.cshtml
    ~/Views/RegistrationFormSurface/USNAdvancedPageComponents/TDN_MemberProfile.vbhtml
    ~/Views/Shared/USNAdvancedPageComponents/TDN_MemberProfile.cshtml
    ~/Views/Shared/USNAdvancedPageComponents/TDN_MemberProfile.vbhtml
    

    And to make things clear this is the path to my partial file in Root folder

    Root\Views\Partials\USNAdvancedPageComponents\TDN_MemberProfile
    

    My controller path

    Root\App_Code\USNControllers\RegistrationFormSurfaceController.cs
    
  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Nov 10, 2016 @ 07:03
    David Brendel
    1

    Hi athul,

    as Dennis mentioned mvc can't find the view as the path is not correct. But instead of adding the whole path I would change the return to

    return PartialView(model)
    

    and then move the view TDN_MemberProfile.cshtml directly to the partial folder. In my opinion that would be the correct way where to store the view.

    Regards David

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Nov 10, 2016 @ 06:50
    Dennis Adolfi
    100

    Unless you want to have all your partial components in one place, then you should keep it where it is and in your controller return:

    return View("/Views/Partials/USNAdvancedPageComponents/TDN_MemberProfile.cshtml", model);

Please Sign in or register to post replies

Write your reply to:

Draft