Question

Main Page -> Section 1 (has some dropdowns and a save button)

        <div id="tab-section1">
            @{Html.RenderPartial("_Section1", Model.Section1);}
        </div>
        <div id="tab-section2">
            <div id="section2">
                @{Html.RenderPartial("_Section2", Model.Section2);}
            </div>
            @{Html.RenderPartial("_SubSection2", Model.SubSection2);}
        </div>

The section 1 contents are placed in a partial view with @Html.BeginForm in it. and rendered on main view using @Html.RenderPartial

@using MyData
@model Section1ViewModel
@using(Html.BeginForm("EditSection1", "Project", FormMethod.Post, new { id = "section1-form", name = "section-form" }))
{
    @Html.HiddenFor(model => model.ProjectID)
 <table id="modules">
        <tr>
            <td class="bold" colspan="2">Modules
            </td>
        </tr>
        <tr>
            <td>
                @Html.DropDownListFor(m => m.SubmittedModules, new MultiSelectList(Model.AvailableModules, "ModuleID", "ModuleName", Model.SelectedModules.Select(m => m.ModuleID)),
                new { multiple = "multiple", @class = "multiselectb" })
            </td>
            <td>
                <input type="button" id="btnAddModule" value=" + " />
            </td>
        </tr>

        @foreach (Module b in @Model.SelectedModules)
        {
            <tr>
                <td colspan="2">
                    @b.ModuleName
                </td>
            </tr>
        }
   </table>
}

When i click the save button in partial view, it should update its own contents as well as other partial view SubSection2 should be refreshed.

In the action method, i return the new values, and for the second partial view updation, I create an ajax submit function where i do the #secondpartialview.load Action:

[HttpPost]
        public ActionResult EditSection1(Section1ViewModel viewModel)
        {
            Section1Data section1Data = new Section1Data(_UnitOfWork);
            // save changes
            section1Data.SaveSection1(viewModel);

            viewModel = section1Data.GetSection1ViewModel(viewModel.ProjectID);
            return PartialView("_Section1", viewModel);
        }

Ajax submit:

$("#section1-form").submit(function () {

        $("#section1-saving").html("<img src='../../Images/ajax-loader.gif' />");

        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function (result) {
                $("#section1-saving").html("Saved!");
                $.ajaxSettings.cache = false;

                // Refresh the sub section 2 on the Section 2 tab
                $("#subSection2").load('../../Projects/subSection2/' + $("#ProjectID").val());
            },
            error: function (jqXHR, textStatus, errorThrown) {
                $("#section1-saving").html("Error: " + textStatus + " " + errorThrown);
            }
        });
        return false;
    });

The issue is: the DEBUGGER shows me the updated values for selectedModules in action method, but NOT on the UI.

What am i missing?

Was it helpful?

Solution

I had this kind of issue when i have multiple Partial view in a single Parent View.What you should do is that, In the Parent View Call Section1 Paritial View like

 <div id="tab-section1">
            @{Html.RenderPartial("_Section1", Model.Section1);}
        </div>

Now in the _Section1 Partial View call your _Section2 Partial View

<div id="section2">
                @{Html.RenderPartial("_Section2", Model.Section2);}
            </div>

Using this Approach of Partial View inside of a Partial View you will be able to get Value from your UI.This is applicable for multiple partial view inside a parent view. Also i like call my partial view like

@Html.Partial("ViewName",Model.ModelName)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top