在局部视图ASP.NET MVC 4 Html.BeginForm,后后没有正确的价值观 [英] ASP.NET MVC 4 Html.BeginForm in Partial View , values after post not right

查看:147
本文介绍了在局部视图ASP.NET MVC 4 Html.BeginForm,后后没有正确的价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主页
          - >第1(有一定的下拉菜单和保存按钮)

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>

的部分1内容被放置在一个局部视图与@ Html.BeginForm在它。
和上呈现的主视图中使用@ Html.RenderPartial

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>
}

当我在局部视图点击保存按钮,它应该更新自己的内容以及其他局部视图SubSection2应该被刷新。

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.

在操作方法中,我回到了新的价值,并为第二次局部视图选择更新,我创建一个AJAX提交功能,在那里我做#secondpartialview.load
动作:

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);
        }

阿贾克斯提交:

$("#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;
    });

问题是:调试器显示我的操作方法selectedModules更新值,但不是在UI

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

我想什么?

推荐答案

我有这样的问题,当我在一个单亲View.What你应该做的是,有多个局部视图,
在父视图呼叫SECTION1滞量查看

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>

现在在_Section1局部视图打电话给你_Section2管窥

Now in the _Section1 Partial View call your _Section2 Partial View

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

使用的局部视图内管窥这种方法,你就可以从你的UI.This获得价值适用于父视图内的多个局部视图。
此外,我想打电话给我的局部视图像

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)

这篇关于在局部视图ASP.NET MVC 4 Html.BeginForm,后后没有正确的价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆