Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2015
    Posts
    2

    C# ASP.NET MVC - @Url.Action/@Html.ActionLink not generating URL

    I've posted on StackOverflow, but had no reply - http://stackoverflow.com/questions/3...urrent-context

    Basically I have a project I'm working on having complete the Comprehensive ASP.NET MVC course. I have login working, I have written several admin screens in an admin area, they all work. I have now started the main page, the controller is populating a ModelView correctly and the Index page can access this and display the data without issue.

    The problem I have is that if I create an anchor and use @Url.Action to populate the href with a URL when the page is displayed the link doesn't work, checking the source the href attribute and URL are missing. If I use @Html.ActionLink again it doesn't work, checking the source the href is there but no URL.

    I cannot for the life of me figure out why.

    I've recreated the page afresh, so there is nothing other than the links...

    Code:
    @model Brettex.ViewModels.OverviewIndex
    
    <h2>
        Overview Screen
    </h2>
    
    <br />
    
    <div class="btn-toolbar">
        <a href="@Url.Action("new")" class="btn btn-primary btn-sm">
            <i class="glyphicon glyphicon-plus"></i>
            Create Company
        </a>
    </div>
    
    @Html.ActionLink("Create Company", "New", "Summary")
    And here is the source of the page...

    Code:
                    <a href="/admin/Machines">Machine Admin</a>
                    <a href="/admin/Readings">Readings Admin</a>
                <a href="/logout">Logout</a>
            
    
            
    <h2>
        Overview Screen
    </h2>
    
    <br />
    
    <div class="btn-toolbar">
        <a class="btn btn-primary btn-sm">
            <i class="glyphicon glyphicon-plus"></i>
            Create Company
        </a>
    </div>
    
    <a href="">Create Company</a>
    The two links at the top come form the _Layout, and as you can see they work fine. The two link that don't work are in a .cshtml page that is rendered by the RenderBody() from the _Layout.

    This is really annoying me now, I've been stuck on this for nearly a week!

    Any help would be great.


    Regards

    Tom

  2. #2
    Join Date
    Aug 2010
    Posts
    160
    Almost identical code and I get an href... no idea why this is not working for you.

    Code:
                            <a href="@Url.Action("edit", new{user.Id})" class="btn btn-xs btn-primary">
                                <i class="glyphicon glyphicon-edit"></i>
                                edit
                            </a>

  3. #3
    Join Date
    Jan 2014
    Location
    Lynnwood, WA
    Posts
    46
    just speculating... but this looks awfully suspicious:
    Code:
        href="@Url.Action("new")"
    Look at am385's code:
    Code:
        href="@Url.Action("edit", new{user.Id})"
    Ostensibly your Url.Action string generates 'new' as the string for the action, but as i recall Nelson complains about actions (or was it actionlinks..?) not going to the right overload readily. To test this you could add: new {} as a 2nd parameter and see if it fixes the issue.

    from my notes i made to myself when doing the course, maybe they're sort of useful...
    Code:
    <p><a href="@Url.Action("index","Posts", new {area="admin"})">View Posts in Admin</a></p>
     // action vs route - used here to move to any arbitrary route it can reverse engineer
    from my note, it would imply the url.action cant figure out how to reverse engineer your link. Is it clear to you that that path actually exists?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •