Copied to clipboard

Flag this post as spam?

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


  • Tom Engan 430 posts 1173 karma points
    Sep 01, 2017 @ 11:11
    Tom Engan
    0

    How to get Id from view into macro?

    I link from a page containing some partial views, with this link:

    <a href="/[email protected]"></a>
    

    Then this link now hits one of the partial view with this macro partial view:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @Html.Action("ReadHikingDestination", "HikingDestinationSurface")
    

    And the action method ReadHikingDestination in the Surfacecontroller is executed:

    [ChildActionOnly]
     public ActionResult ReadHikingDestination(int? Id)
     {
        HikingDestinationViewModel model = new HkingDestinationViewModel();
        if (Id == null)
        {
            //Codes
            return PartialView("CreateHikingDestination", model);
        }
        else
        {
            //Codes
            return PartialView("UpdateHikingDestination", model);
        }
    }
    

    It can distinguish between which partial view to return since int is nullable, and I either get an empty partial form (without id - returns CreateHikingDestination), or a filled partial form (with id - returns UpdateHikingDestination).

    But what if I want to distinguish between these two in the macro instead? How does it look like?

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{if (Id == null)     // <<<< HOW TO FIND THIS ID (this don't work)?
        {
            @*actionName: ReadHikingDestination - controllerName: HikingDestinationSurface*@
            @Html.Action("ReadHikingDestination", "HikingDestinationSurface")
        }
        else
        {
            @*actionName: ReadHikingDestinationById - controllerName: HikingDestinationSurface*@
            @Html.Action("ReadHikingDestinationById", "HikingDestinationSurface")
        }
    }
    

    Something like the macro above, which can distinguish between these two methods instead of, in the Surfacecontroller:

    [ChildActionOnly]
    public ActionResult ReadHikingDestination()
    {
        HikingDestinationViewModel model = new HikingDestinationViewModel();
        //Codes
        return PartialView("CreateHikingDestination", model);
    }
    
    
    [ChildActionOnly]
    public ActionResult ReadHikingDestinationById(int Id)
    {
        HikingDestinationViewModel model = new HikingDestinationViewModel();
        //Codes
        return PartialView("UpdateHikingDestination", model);
    }
    

    <a href="/[email protected]"></a> is used to retrieve data from a single post to be edited (/turmaal without id is used to get a blank form). Should I rather use an ActionLink or other method instead of a regular a-tag in the view?

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Sep 01, 2017 @ 16:23
    Frans de Jong
    1

    In the developer menu you'll need to find your macro and add a parameter to is that is called id. Now in you'r Macro you can use

    var id = Model.MacroParameters["id"];
    

    The only thing missing now is getting the parameter in the macro and that can be done like this:

    @Umbraco.RenderMacro("someMacro", new {id=hikingdestination.Id})
    

    Does this answer your question?

  • Tom Engan 430 posts 1173 karma points
    Sep 01, 2017 @ 16:58
    Tom Engan
    0

    Thank you so much, I think so. I will give feedback as soon as I've tested this, but it may be over the weekend - no later than Monday. :-)

  • Tom Engan 430 posts 1173 karma points
    Sep 04, 2017 @ 13:19
    Tom Engan
    0

    I think I understand the point here, but I have to combine RenderMacro and MacroParameters differently, as I've designed my page (with a combined create/update form abowe and a link list with update/delete below). I'll try if I can use a similar solution eventually. Either way, this is useful information that may be useful later.

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Sep 04, 2017 @ 13:32
    Frans de Jong
    0

    How and where do you refer to your partial view macro? I thought you were using a partial view macro already.

  • Tom Engan 430 posts 1173 karma points
    Sep 04, 2017 @ 14:28
    Tom Engan
    0

    My view looks like this. The macros CreateHikingDestination and ListHikingDestination are inside of a grid in this page. If the edit link in the list, the left side contains an Id (<a href="/[email protected]">), then the CreateHikingDestination macro is replaced with UpdateHikingDestination macro (macros and partial view got the same names):

    NOTE: I may only point to the action ReadHikingDestination from macro CreateHikingDestination right now described at the top of the page now. I'm a bit uncertain right now. I've had little time to check through everything now at the writing moment. There is a lot of information to be written if I should display all the information in all macros, views and the surfacecontroller.

    enter image description here

    Partial View ListHikingDestination:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IEnumerable<HikingDestinationViewModel>>
    @using Neoweb.Models
    
    @using (Html.BeginUmbracoForm<Neoweb.Controllers.HikingDestinationSurfaceController>("ListHikingDestinationsByUserName"))
    {
    <fieldset>
        <div class="form-group">
            <table>
                <tr>
                    <th style="padding-left: 5px"></th>
                    <th></th>
                    <th>Turmål</th>
                    <th>Startdato</th>
                    <th>Turkode</th>
                    <th>Status</th>
                </tr>
                @if (!Model.Any())
                {
                    <tr>
                        <td style="padding-left: 10px">Ingen turmål registrert</td>
                        <td colspan="4"></td>
                    </tr>
                }
                @foreach (var hikingdestination in Model)
                {
                    <tr>
                        <td style="padding-left: 10px; padding-right:5px"><a href="/[email protected]"><span class="fa fa-pencil-square-o"></span></a></td>
                        <td style="padding-right: 10px">@Html.ActionLink(" ", "DeleteHikingDestination", new { Id = @hikingdestination.Id }, new { @class = "fa fa-trash-o", onclick = "return confirm('Er du sikker på du vil slette turmålet?');" })</td>
                        <td><a href="@Html.DisplayFor(m => @hikingdestination.HikingDestination.Url)">@Html.DisplayFor(m => @hikingdestination.HikingDestination.Name)</a></td>
                        <td>@Html.DisplayFor(m => @hikingdestination.StartDate)</td>
                        <td>@Html.DisplayFor(m => @hikingdestination.HikingCode)</td>
                        <td><span title="Korrekt turkode" class="fa fa-check-square-o"></span> <span title="Turkode ikke korrekt" class="fa fa-times"></span> <span title="Turkode mangler" class="fa fa-square-o"> </span></td>
                    </tr>
                }
            </table>
        </div>
    </fieldset>
    }
    

    The Updatelink is this link abowe:

    <td style="padding-left: 10px; padding-right:5px"><a href="/[email protected]"><span class="fa fa-pencil-square-o"></span></a></td>
    
  • Tom Engan 430 posts 1173 karma points
    Sep 05, 2017 @ 09:06
    Tom Engan
    0

    I guess I can't use @Umbraco.RenderMacro("someMacro", new {id = hikingdestination.Id}) inside of a grid, so I do not think this method can be used directly in my case. Is that right?

    But I can also distinguish between create and update from the querystring, and then I found that this solution also works, and can distinguish between the two different action methods ReadHikingDestination and ReadHikingDestinationById.

    This is now the content of partial view macro CreateHikingDestination, and partial view macro and macro UpdateHikingDestination becomes redundant (I have now renamed CreateHikingDestination to HikingDestinationForm since it now also contains update functionality).

    @{
        if (Request.QueryString["Id"] == null)
        {
            @Html.Action("ReadHikingDestination", "HikingDestinationSurface")
        }
        else
        {
            @Html.Action("ReadHikingDestinationById", "HikingDestinationSurface")
        }
    }
    

    Are there any weaknesses in using querystring instead of macro parameters?

Please Sign in or register to post replies

Write your reply to:

Draft