Search code examples
asp.net-mvc-4asp.net-mvc-partialviewumbraco6

ViewBags using @Html.Action to render form on partial view


View:

<p>Parent ViewData: @ViewData["Test"]</p>   

@Html.Action("MemberSignup","MemberSignupSurface")

PartialView:

<p>PartialView ViewData: @ViewData["Test"]</p>  

@using (Html.BeginUmbracoForm<MemberSignupSurfaceController>
    ("MemberSignupSubmit", "MemberSignupSurfaceController",FormMethod.Post))
{
    <!-- Some form controls -->
    <input type="submit" value="Signup" />
}

Controller:

public class MemberSignupSurfaceController : SurfaceController
{
    public ActionResult MemberSignup()
    {
        ViewData["Test"] = "From MemberSignup";
        // Do database stuff and create model from that
        return PartialView("MemberSignupView", model);
    }


    [HttpPost]
    public ActionResult MemberSignupSubmit(MemberViewModel model)
    {
        ViewData["Test"] = "From MemberSignupSubmit";

        if (ModelState.IsValid)
        {
             // Redirect to success using TempData
        }
        else
        {
            return CurrentUmbracoPage();
        }                                            
    }       
}

When my page load MemberSignup is called and the page shows

Parent ViewData:

PartialView ViewData: From MemberSignup

Then when i submit the form on the partial view with invalid input so it won't validate and it calls CurrentUmbracoPage() in the action MemberSignupSubmit

I get the following:

Parent ViewData: From MemberSignupSubmit

PartialView ViewData: From MemberSignup

If i use @Html.Partial to render my partial view both viewbags shows the same value set from the submit action.

I've tried TempDatabut it is not working either. Is there really no way to pass anything back to the partial view after i return from the submit action when using @Html.Action to render a partial view form.

The overall problem I am trying to solve is to popluate a dropdown in my form with values from the database. Using @Html.Partial don't allow me to do this but have a working viewbag.


Solution

  • I did this to render a dynamic dropdown list with values from a database. Maybe it will help someone.

    It is a music player which needs a dynamic db populated menu to list the playlists

    I made a base controller which all other controllers inherit from. In that base class, I have a PlaylistPopupMenu action which gets the list of playlists from a db.

        public PartialViewResult PlaylistPopupMenu()
        {
            try
            {
                return PartialView("_PlaylistPopupMenu", db.GetPlaylists(1).ToList());
            }
            catch (Exception)
            {
                throw;
            }
        }
    

    Then I have a _PlaylistPopupMenu partial view as follows:

    @model List<OneMusic.Models.GetPlaylists_Result>
    @if (Model.Count > 0)
    {
        <li style="height:2px" class="divider"></li>
        foreach (var item in Model)
        {
            <li style="height:30px">@Html.DisplayFor(p => item.Name)
                @Html.ActionLink(item.Name, "AddSong", "Playlist", new { playlistId =       @item.PlaylistId, songId = 1 }, "") 
           </li>
        }
    }
    

    this renders the dynamic parts of the menu (ie the playlists)

    Finally the main page has this to build the dynamic part of the menu:

    <ul class="dropdown-menu" style="margin-top:10px"><p class="text-primary" style="margin-left:18px; margin-top:6px">Add To</p>
    
        <!-- other static menu items here-->
    
        <li style="margin-top:-60px; height:0px">@Html.Action("PlaylistPopupMenu")</li>
    </ul>