在MVC中添加动态表单内容

mue*_*uek 4 asp.net-mvc jquery dynamic-content asp.net-mvc-2

我正在尝试将动态内容添加到我的mvc应用程序中.

我正在使用Steven Sanderson发布编辑可变长度列表,ASP.NET MVC 2样式,并且由于它,我有一些动态内容.由于我的应用程序的一些限制,我不得不改变基础礼品类.

问题是现在应用程序无法正常工作.

在我的模型中我有:

public class Gift
{
    //public string Name { get; set; }
    //public double Price { get; set; }

    public List<string> MyList { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在,我在gifts参数中得到null值

[HttpPost]
public ActionResult Index(IEnumerable<Gift> gifts)
{
    return View("Completed", gifts);
}
Run Code Online (Sandbox Code Playgroud)

谁可以帮我这个事?

编辑: 这是我的查看代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Enviro2011.Models.Gift>>" %>

<asp:Content ContentPlaceHolderID="HeadContent" runat="server">
    <link rel="Stylesheet" href="../../Content/styles.css" />
    <script type="text/javascript">
        $(document).ready(function () {

            $("#addItem").click(function () {
                $.ajax({
                    url: this.href,
                    cache: false,
                    success: function (html) { $("#editorRows").append(html); }
                });
                return false;
            });

            $("a.deleteRow").live("click", function () {
                $(this).parents("div.editorRow:first").remove();
                return false;
            });
        });
    </script>
</asp:Content>
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Gift List</h2>
    What do you want for your birthday?
    <% using (Html.BeginForm())
       { %>
    <div id="editorRows">
        <% foreach (var item in Model)
               Html.RenderPartial("GiftEditorRow", item);
        %>
    </div>
    <%: Html.ActionLink("Add another...", "Add", null, new { id = "addItem" }) %>
    <input type="submit" value="Finished" />
    <% } %>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)

Edit2 我也从Phil Haack 发现了模型绑定到列表的帖子,并且工作得很好,但我无法实现"添加书"功能!

Rap*_*Rap 5

问题是你在模型中循环而不是模型中的项目.

这是你可以做的:

<% foreach (var item in Model.MyList)  // <-- Add the ".MyList"
       Html.RenderPartial("GiftEditorRow", item);
%>
Run Code Online (Sandbox Code Playgroud)

我们假设您的局部视图只接收一个字符串.

至于你的Ajax调用不起作用,问题在于你尝试提交两次,一次使用actionLink,另一次使用Ajax.将ActionLink更改为显式链接,后面没有任何操作:

<a id="addItem">Add another ...</a>
Run Code Online (Sandbox Code Playgroud)

然后你的jQuery Ajax调用将会激活.