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
    Jan 10, 2020 @ 09:05
    Lasse
    0

    How to get to another view with a link and MVC

    Hello,

    I have a list of objects and I want to be able to select a specific one and view a different view with specific information regarding the object

    e.g. A list of bikes, but you want to see specific information regarding a bike, if it is broken or whatever.

    How do I go from one view to another, using a link (could be actionlink) and still using current layout using a controller and method. I have semi solved the issue, but when I open the link it opens a blank page with what I have, no navigation bar or anything.

    Any ideas?

    This is what i have in my current vieiw: <td>@Html.ActionLink("LinkText", "ControllerName", "MethodName", new { id = Something }, null)</td>

    Edit: I have correct controller, method name and ID in my view, this is just an example

    This is my result:

    enter image description here

  • Lasse 49 posts 161 karma points
    Jan 10, 2020 @ 11:47
    Lasse
    0

    Anyone got any ideas, i am running low on google search options to fix this

  • Graham Davis 110 posts 376 karma points
    Jan 13, 2020 @ 04:53
    Graham Davis
    0

    When you click the link, is it supposed to go to a new page or load a div with a partial view on the current one?

  • Lasse 49 posts 161 karma points
    Jan 13, 2020 @ 07:00
    Lasse
    0

    The ActionLink i have right now, is the only way i know of, to get a Call to action button / Linktext, it should open in a new page but within the same template.

  • Graham Davis 110 posts 376 karma points
    Jan 13, 2020 @ 14:00
    Graham Davis
    0

    If I understand you correctly, you have some information in a database (about Bikes) that you want to load dynamically to a page using its id. If so, this is how am doing it for real estate listings. The way to do it is different between versions 7 and 8, so I am including code for both.

    Side Note..... you may have to create a custom route.

    ******************
    *** UMBRACO 7  ***
    ******************
    
    
    ***** The Controller *****
    
    
    using System.Globalization;
    using System.Web.Mvc;
    using Umbraco.Core.Models;
    using Umbraco.Web;
    using Umbraco.Web.Models;
    
    
    namespace zbdpm.Controllers
    {
        //[OutputCache(NoStore = true,  Duration = 0)]
        public class PropertyController : Umbraco.Web.Mvc.SurfaceController
        {
           public PropertyController()
                : this(UmbracoContext.Current)
            {
            }
            public PropertyController(UmbracoContext umbracoContext)
                : base(umbracoContext)
            {
            }
            private RenderModel CreateUmbracoModel()
            {
                //use and existing node
                umbraco.NodeFactory.Node node = umbraco.uQuery.GetNode(-1).Children[0];
                IPublishedContent currentNode = Umbraco.TypedContent(node.Id);
                var model = new RenderModel(currentNode, CultureInfo.CurrentUICulture);
                //add an umbraco data token so the umbraco view engine executes
                RouteData.DataTokens["umbraco"] = model;
                return model;
            }
    
            public ActionResult Index(string propertyID)
            {
                PropertyDetails p = new PropertyDetails();
                p = z.zpmAPI(propertyID).jsonToEntity<PropertyDetails>();
    
                if (p._Property.PropertyID == null)
                {
                    return View("/Views/Partials/Property/real-estate-not-found.cshtml", CreateUmbracoModel());
                }
                else
                {              
    
                    ViewBag.PropertyDetails = p;//using viewbag to pass data through the umbraco model
                    return View("/Views/Partials/Property/real-estate.cshtml", CreateUmbracoModel());
                }
            }
            public ActionResult PropertyDetails(PropertyDetails p)
            {
                return PartialView("~/Views/Partials/Property/_zProperty.cshtml", p);
            }
        }
    }
    
    
    
    ***** real-estate.cshtml *****
    
    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    <div class="container-fluid">
        <div class="container">
            @Html.Action("PropertyDetails", "Property", (dbzBestPM.Models.PropertyDetails)ViewBag.PropertyDetails)
        </div>
    </div>
    
    
    
    ***** zProperty.cshtml *****
    
    @model dbzBestPM.Models.PropertyDetails
    
    <div id="Section-PropertyDetails">
        <div class="row">
            <div class="col-xs-12 col-sm-8 col-md-8 col-lg-8 col-xl-8">
                <h1>@Model._Property.StreetAddress.Trim()  @Model._Property.City.Trim(),  @Model._Property.State.Trim()   </h1>
    
            </div>
        </div>
    
        ******* PAGE HTML HERE *****
    </div>
    
  • Graham Davis 110 posts 376 karma points
    Jan 13, 2020 @ 14:01
    Graham Davis
    0
    ******************
    *** UMBRACO 8  ***
    ******************
    
    ***** Extended Surface Controller *****
    
    
    using Umbraco.Web.Models;
    using System.Linq;
    
    public partial class zBestExtendedSurfaceController : Umbraco.Web.Mvc.SurfaceController
    {
        public ContentModel CreateUmbracoModel()
        {
            try
            { 
                //Borrow the Root for Creating Dynamic content pages
                return new ContentModel(Umbraco.ContentAtRoot().FirstOrDefault());        
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }
        public ContentModel CreateUmbracoModel(string pageName)
        {
            try
            {
                var cm = Umbraco.ContentQuery.Search(pageName)
                    .ToList()
                    .Where(x => x.Content.Name.ToLower() == pageName.ToLower())
                    .FirstOrDefault()
                    .Content;
                return new ContentModel(cm);
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }
    }
    
    ***** The Controller *****
    
    using System.Web.Mvc;
    
    namespace zBestPM.Controllers
    {
        [OutputCache(NoStore = true,  Duration = 0)]
        public class PropertyController : zBestExtendedSurfaceController
        {
            public ActionResult Index(string propertyID)
            {
    
                PropertyDetails p = new PropertyDetails();
                p = z.zpmAPI(propertyID).jsonToEntity<PropertyDetails>();
    
                if (p == null)
                {
                    return View("/Views/Partials/Property/real-estate-not-found.cshtml", CreateUmbracoModel());
                }
                else
                {                
                    Session["PropertyInfo"]= p;//Using Session Variable to Pass data to page
                    return View("~/Views/PropertyInfo.cshtml", CreateUmbracoModel("real-estate"));//real-estate is defined in the umbraco backoffice
                }
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft