views:

41

answers:

2

I'm trying to implement a Widget control that exists on every page in the system, which will allow the user to have basic Search & Directory functionality available on each page. This is a tab control defined below, where in the <ul> the currently selected tab is determined by the the value in Model.CurrentTab and the corresponding content that I want to display (basically, make visible) is also determined by that value.

<div class="WidgetControl">

    <ul class="WidgetTab tabs">
        <li <%= (Model.CurrentTab == "Search") ? "class='active'" : "" %>>
            <span href='<%= Url.Action("SearchBox", "Search") %>'>Search</span>
        </li>

        <li <%= (Model.CurrentTab == "Directory") ? "class='active'" : "" %>>
            <span href='<%= Url.Action("DirectoryList", "Group") %>'>Directory</span>
        </li>
    </ul>

    <div id="Search" class="tab_container">
        <% Html.RenderAction("SearchBox", "Search"
                    , (Model.CurrentTab == "Search") ? Model.Search : ""); %>
    </div>

    <div id="Directory" class="tab_container">
        <% Html.RenderAction("DirectoryList", "Group"
                    , (Model.CurrentTab == "Directory") ? Model.Search : ""); %>
    </div>
</div>

The reason I want to load both Search and Directory is so the page doesn't have to request the content depending on which tab is clicked. I want it all to be available immediately.

The problem I'm having is that if CurrentTab contains the value "Directory" this means (I assumed) that Html.RenderAction("SearchBox"... should pass in an empty string. But when it gets to the action method, the View Model passed into SearchBox contains a value and not ""

I don't understand why this is happening. Even when I pass an empty string into SearchBox, the View Model still contains a value. Can somebody please explain what's going on? Should I be doing this differently?

update:

    public PartialViewResult DirectoryList(DirectoryViewModel vm)
    {
        return PartialView(vm.Search); // this is expecting a string
    }

    public PartialViewResult SearchBox(SearchViewModel vm)
    {
        return PartialView(vm); // the among other things, the Search string is used
    }

Both DirectoryViewModel and SearchViewModel contain a property called Search

A: 

Should you be doing something like this

 <% Html.RenderAction("SearchBox", "Search", 
                    new { vm = ((Model.CurrentTab == "Search") ? Model.Search : "") }); %>

Since the third parameter of the Html.RenderAction is object routeValues which is a dictionary with a parameter of the Action you are calling as a Key. If you don't specify the parameter that you are passing in your routeValues parameter of your Html.RenderAction it will always pass an object value to the vm parameter of your Action.

rob waminal
+1  A: 

The ModelBinder will new() up any object in a ActionMethod's parameters. I don't think this behavior can be turned off without implementing your own modelbidner. You need to create an overload that has no parameters and route it accordingly.

jfar