Copied to clipboard

Flag this post as spam?

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


  • Brett Spencer 88 posts 258 karma points
    Jul 13, 2017 @ 22:25
    Brett Spencer
    0

    Getting <a href>No Url</a> from ActionLink in Views

    I've been searching online for hours and trying different things all-day.

    As the title states, I cannot get @Html.Actionlink or the @Url.Action to generate a url. I have no idea why:

    So, here is my setup:

    Controller (BaseMvcController inherits RenderMvcController)

    public class BlogContainerController : BaseMvcController
    {
        public override ActionResult Index(RenderModel umbracoModel)
        {
            BlogContainer model = GetViewModel(umbracoModel);
    
            return CurrentTemplate(model);
        } 
    

    }

    I cannot use return base(model); because my model is not inheriting RenderModel.

    I am also using the Model Builder and extending them with my own partial classes:

    Model

        /// <summary>BlogContainer</summary>
    [PublishedContentModel("blogContainer")]
    public partial class BlogContainer
    {
    
        public new const string ModelTypeAlias = "blogContainer";
        public new const PublishedItemType ModelItemType = PublishedItemType.Content;
    
        public BlogContainer(IPublishedContent content)
            : base(content)
        { }
    

    //....

    public partial class BlogContainer : PublishedContentModel
    {
        public IEnumerable<BlogArticle> ArticleList { get; set; }
    
        public IEnumerable<BlogYearFolder> BlogYears { get; set; }
    
        public IEnumerable<TagModel> TagList { get; set; }
    
    }
    

    Initially, I was using a partial view for this part but trying to troubleshoot, I placed it back in the parent view. I need to be able to pass in variables for the tags and attempted that first but no matter what I do - no url...

    View

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<BlogContainer>
    <div>
        <h3>Tags</h3>
        <ul>
            @foreach (var tag in Model.TagList)
                {
                <li>
                    @Html.ActionLink(tag.Text, "Index", "BlogContainer", null , null)
                <a href="@Url.Action("Index", "BlogContainer", null)"> test url</a>
            </li>
    
            }
        </ul>
    </div>
    

    This is the HTML that is rendered:

    Page

        <div>
        <h3>Tags</h3>
        <ul>
                <li>
                    <a href="">Brett Spencer</a>
                <a> test url</a>
            </li>
                <li>
                    <a href="">Tag 1</a>
                <a> test url</a>
            </li>
                <li>
                    <a href="">Tag2</a>
                <a> test url</a>
            </li>
        </ul>
    </div>
    

    Why is this not generating a url?

  • Brett Spencer 88 posts 258 karma points
    Jul 14, 2017 @ 16:52
    Brett Spencer
    100

    It seems the only way to generate a url is to use a Surface controller - that opens up a whole bunch of issues.

    Now I can't easily use return CurrentTemplate(model);. I tried customizing the EnsurePhsyicalViewExists(template) function but that turned out to be way more work as I have to hijack the ViewEngine because Umbraco doesn't stick to the MVC pattern and places all of the Templates under the root of Views.

    And my routing looks terrible:

    http://localhost:1973/umbraco/Surface/BlogSurface/Blog?pageId=2244&tag=Brett%20Spencer

    So to return the original Template or View (Blog), I had to magically string the view in the Surface Controller - return View("~/Views/Blog.cshtml", model);

    It seems it is impossible to create an ActionLink() unless your using a Surface Controller!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies