Copied to clipboard

Flag this post as spam?

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


  • J 447 posts 864 karma points
    Dec 02, 2021 @ 15:22
    J
    0

    Is it possible to pass in a value from Html.Partial?

    I have a simple MVC view where i would like to display or pass in a value from a template to the control and take the appropriate action. Here is the template

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
        Layout = "master.cshtml";
    }
    
    @* the fun starts here *@
    
    @Html.Partial("~/Views/Partials/BusinessVenture/Index.cshtml", new BusinessVenture())
    

    The page runs and displays as required.

    There are 2 types Business ventures, Fun Activity or Placements.

    I have a public property in my code behind

    public string BusinessVentName {get; set;};
    

    How can i pass the value from the template to the backend code, so if i set it to FunActivity in the template it would take the appropriate action?

    I tried adding a parameter to the controller as below but everything i tried resulted in some syntax error.

    @Html.Partial("~/Views/Partials/BusinessVenture/Index.cshtml", new BusinessVenture(BusinessVentName="FunActivity"))
    

    Is it possible to do this?

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Dec 02, 2021 @ 17:11
    Huw Reddick
    1

    try

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
        Layout = "master.cshtml";
        var businessventure = new BusinessVenture();
        businessventure.BusinessventName = "FunActivity";
    }
    
    @* the fun starts here *@
    
    @Html.Partial("~/Views/Partials/BusinessVenture/Index.cshtml", businessventure)
    
  • Lucas Michaelsen 32 posts 233 karma points
    Dec 02, 2021 @ 20:38
    Lucas Michaelsen
    100

    Hey J, sure you can do that.

    Your view

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
        Layout = "master.cshtml";
    }
    
    @* the fun starts here *@
    
    @Html.Partial("~/Views/Partials/BusinessVenture/Index.cshtml", new BusinessVenture() { 
          BusinessVentName = "FunActivity"
    })
    

    your partial

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<BusinessVenture>
    <h1>@Model.BusinessVentName</h1>
    
  • 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