Copied to clipboard

Flag this post as spam?

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


  • pat 124 posts 346 karma points
    Apr 18, 2017 @ 13:12
    pat
    0

    how to add custom mvc form using surface controller and Partial view macro

    Hi all, Please could anyone explain me how to create a view and surfacecontroller and then add to a page using partial view macro file.

    I have created surface controller code as below

    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    using myicCodeProject.Models;
    
    namespace myicCodeProject.Controllers
    {
        public class ListSitesController : SurfaceController 
        {
            // GET: ListSites
            public ActionResult Index()
            {
                if (Umbraco.MemberIsLoggedOn())
                {
                    TempData["test2"] = "member logged in";
                }
                var mysites = new List<SiteModel>();
    
                var s = new SiteModel();
                s.name = "ABC ltd";
                s.shortname = "Main";
                s.city = "Luton";
                s.postcode = "LL2 4GH";
                mysites.Add(s);
    
                var s1 = new SiteModel();
                s1.name = "JCob and Sons ltd";
                s1.shortname = "Main";
                s1.city = "Denver";
                s1.postcode = "DB2 4GB";
                mysites.Add(s1);
    
                var s2 = new SiteModel();
                s2.name = "Desi ltd";
                s2.shortname = "Logistics";
                s2.city = "Necastle";
                s2.postcode = "NW23 4FG";
                mysites.Add(s2);
    
    
    
                TempData["sitelist"] = mysites;
                ViewBag.sites = mysites;
                return View();
            }
        }
    }
    

    created model call SiteModel and build the project copy dll file in umbraco web site bin folder.

    Then I have created view Index.cshtml in folder ListSites and placed it in Views/Partails/ folder in umbraco web site

    this is the code on view

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<myicCodeProject.Models.SiteModel>
    @using Umbraco.Web
    @using System.Web.Mvc.Html
    @if (Umbraco.MemberIsLoggedOn())
    {
        var list = new List<myicCodeProject.Models.SiteModel>();
        list = (List<myicCodeProject.Models.SiteModel>)TempData["sitelist"];
    
        <br />
        @TempData["test2"]
    
        if (list != null)
        {
    
            if (list.Count > 0)
            {
                <table>
                    <tr>
                        <td>Name</td>
                        <td>Short name</td>
                        <td>City</td>
                        <td>Post Code</td>
    
                    </tr>
                    @foreach (var item in list)
                    {
                        <tr>
                            <td>@item.name </td>
                            <td>@item.shortname</td>
                            <td>@item.city</td>
                            <td>@item.postcode</td>
    
                        </tr>
    
                    }
    
                </table>
            }
            else
            {
                <p>No Sites to list</p>
    
            }
        }
        else
        {
            <p>No Sites</p>
        }
    }
    else
    {
        Session.Add("NeedRedirect", Request.Url.ToString());
        { Html.RenderPartial("~/Views/Partials/myicCodeProject/icLogin.cshtml", new myicCodeProject.Models.icLoginModel(), new ViewDataDictionary { { "mymsg", "1" } }); }
    
    
    }
    

    then created partial macro file and call Render Partial method

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using myicCodeProject.Models
    @using myicCodeProject.Controllers
        @{Html.RenderPartial("~/Views/Partials/listSites/Index.cshtml",new SiteModel());}
    

    then i have added macro in to a RTE ina page , but I cannot see any sites listing on the page it says No Sites. enter image description here that mean controller Index method not reached at all isn't it ?

    How to fix this please ?

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Apr 18, 2017 @ 13:21
    Alex Skrypnyk
    0

    Hi Pat

    You have to use rendering ASP.Net MVC ChildAction of your controller, try this code:

    @Html.Action("Index", "ListSites", new SiteModel() })
    

    Read please great documentation here:

    https://our.umbraco.org/documentation/reference/templating/mvc/forms/tutorial-child-action

    Thanks,

    Alex

  • pat 124 posts 346 karma points
    Apr 18, 2017 @ 14:03
    pat
    0

    Hi Alex , that load the page no errors now but not listing any thing either ..

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Apr 18, 2017 @ 14:05
    Alex Skrypnyk
    0

    Hi Pat

    Try to use full path to your view in controller:

    return View("~/Views/Partials/listSites/Index.cshtml");
    

    Thanks,

    Alex

  • pat 124 posts 346 karma points
    Apr 18, 2017 @ 14:43
    pat
    0

    No luck still shows blank page... i have just pass int to see it not loading any thing

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Apr 18, 2017 @ 14:44
    Alex Skrypnyk
    0

    Pat, if you are going to debug solution? do you see what controller does?

  • pat 124 posts 346 karma points
    Apr 18, 2017 @ 16:21
    pat
    0

    how to debug solution ? Don't I need to debug web page on load ?

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Apr 18, 2017 @ 16:22
    Alex Skrypnyk
    0

    you are right, debug solution on page load

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Apr 18, 2017 @ 16:27
    Alex Skrypnyk
    0

    I'm trying to understand where is the problem, is controller debugging?

  • pat 124 posts 346 karma points
    Apr 19, 2017 @ 08:31
    pat
    0

    Alex , If I add RenderPartial option in macro I get empty page , mean not reaching Inddex method in conroller...

    If I add HTMl.action @{Html.Action("Index", "ListSites",new SiteModel());} in to macro i get below error
    Server Error in '/' Application.

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

    If I put the page in template it works fine

    do i need to update any settings in umbraco /web config .... help would be highly appreciated

  • pat 124 posts 346 karma points
    Apr 19, 2017 @ 09:26
    pat
    0

    this is what i can see enter image description here

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Apr 19, 2017 @ 09:34
    Alex Skrypnyk
    0

    Pat, you have to use custom path to the view - View("~/Views/Partials/listSites/Index.cshtml");

    Because your view is in an unusual place.

    You can easily extend the WebFormViewEngine to specify all the locations you want to look in:

    http://stackoverflow.com/a/909594/2123476

    Thanks,

    Alex

  • pat 124 posts 346 karma points
    Apr 19, 2017 @ 12:01
    pat
    0

    Hi Alex thanks for helping but unfortunately that didn't work either , I have changed Html.Action to custom path

    A public action method '~/Views/Partials/ListSites/Index.cshtml' was not found on controller 'myicCodeProject.Controllers.ListSitesController'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    I have a huge portal written in webforms layout trying to convert it to MVC.....

    Exception Details: System.Web.HttpException: A public action method '~/Views/Partials/ListSites/Index.cshtml' was not found on controller 'myicCodeProject.Controllers.ListSitesController'. enter image description here

Please Sign in or register to post replies

Write your reply to:

Draft