【问题标题】:MVC Get model data posted from dynamically created partial views to controllerMVC 获取从动态创建的局部视图发布到控制器的模型数据
【发布时间】:2013-12-14 01:04:43
【问题描述】:

如何将动态创建的局部视图模型数据发布到控制器?

我的模型类包装器:

namespace Diabuddies.DAL
{
    public class Exer_Main
    {
        public Exer_Workout Workout { get; set; }
        public Exer_Routine Routine { get; set; }
        public Exer_Set Set { get; set; }
    }
}

我的控制器生成以下视图(片段):

@model Diabuddies.DAL.Exer_Main
<body>

@using(Html.BeginForm())
{ 
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset style="width:800px">
        <legend>Workout</legend>
        <div style="float:left;text-align:right">
            <table>
                <tr style="justify-content:center">
                    <td>
                        Workout Name:
                    </td>
                    <td>
                        @Html.TextBoxFor(x => x.Workout.Name, new { style = "width:100%" })
                    </td>
                </tr>
                <tr>
                    <td>
                      Description:
                    </td>
                    <td>
                       @Html.TextAreaFor(x => x.Workout.Description)
                    </td>
                </tr>
                <tr>
                    <td>
                        Wrokout Notes:
                    </td>
                    <td>
                        @Html.TextAreaFor(x => x.Workout.Notes)
                    </td>

                </tr>
                   </table>
             <br />


            <div id="AddItem">Add Routine</div>
            <div id="RoutineRows">
              </div>

            <p>
                <input type="submit" value="Create" />
            </p>
        </div>
    </fieldset>
}
<script>
    $(document).ready(function(){
    $("#AddItem").click(function () {
        //alert("Handler for .click() called.");
        $.get( '@Url.Action("AddRoutineHTML", "Default", new { id = "ert" })', function(data) {
            $('#RoutineRows').append(data);
});
    });
    });

</script>

每次用户单击“添加行”时,都会添加以下部分视图:

@model Diabuddies.Models.Exer_Routine

<fieldset>
    <legend>Routine</legend>
    <table>
        <tr style="justify-content:center">
            <td>
                Routine Name:
            </td>
            <td>
                @Html.TextBoxFor(r => r.Name, new { style = "width:100%" })
            </td>
        </tr>
        <tr>
            <td>
                Description:
            </td>
            <td>
                @Html.TextAreaFor(x => x.Description)
            </td>
        </tr>
        <tr>
            <td>
                Notes:
            </td>
            <td>
                @Html.TextAreaFor(x => x.Notes)
            </td>

        </tr>
    </table>
    </fieldset>

问题来了:如何将动态创建的局部视图发布到控制器?现在我有:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult NewWorkout([Bind(Prefix = "Workout", Include = "Name, Description")]Exer_Workout eWO, List<Exer_Routine> eR)
        {
            //Exer_Workout stuff returns fine, I am lost on how to get the partial view data here.
            Response.Write(eR.Count); //Program breaks here, nulled out.  Obv list isn't answer
            return View();
        }

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    列表是正确的答案。这就是默认模型绑定如何与复杂对象列表一起使用。您将需要在输入名称属性中进行数组索引,如下所示:

    <input type="text" name="Exer_Routine[0].Name" />
    

    对于每个加载的部分,您需要将索引增加 1。您可能需要编写自定义 HTML 而不是使用帮助程序。我建议通过硬编码列表并让模型绑定首先工作来尝试一下。然后你就可以弄清楚如何生成动态HTML了。

    希望这会有所帮助。

    【讨论】:

    • 试过但似乎没有用。我刚刚将以上内容添加到我的部分视图中,并尝试传入单个实例 - 列表仍然为空。我在局部视图中的模型是否仍应相同?还是改成列表?
    • 当您查看源代码时,您输入的 HTML 是否与上述类似: 这是 [0]使列表模型绑定起作用的重要部分。
    • Phil Haack 的这篇文章也值得一读:haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
    • 你的答案是正确的。然而,我必须做的是添加:[Bind(Prefix="Routine", Include="Name, Notes, Description")]List eR - 到 ActionResult,并使用相同的在我的主要观点中建立模型(我的错误也是如此)。如果其他人偶然发现这一点,如果您可以在上面的答案中包含“绑定”,将不胜感激。谢谢马克!!很想知道为什么绑定必须在那里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多