4

I'm trying to add dynamic content to my mvc application.

I'm using Steven Sanderson post Editing a variable length list, ASP.NET MVC 2-style, and thanks to it, I had some dynamic content into it. Due some constraints of my application, I had to make a change into base Gift class.

The problem is that now the application doens't work.

In my Model I have:

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

    public List<string> MyList { get; set; }
}

And now, I'm getting null values in gifts parameter

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

Can anyone help me on this?

EDIT: Here is my View code:

<%@ 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>

Edit2 I also have found out the post Model Binding To A List from Phil Haack, and works just fine, but I can't implement the feature 'Add Book' !!!

4

2 に答える 2

4

問題は、モデル内のアイテムはなく、モデルをループしていることです。

できることは次のとおりです。

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

部分ビューは文字列のみを受け取ると想定します。

Ajax呼び出しが機能しない限り、問題は、2回送信しようとしていることです。1回はactionLinkを使用し、もう1回はAjaxを使用します。ActionLinkを、背後にアクションがない明示的なリンクに変更します。

<a id="addItem">Add another ...</a>

次に、jQueryAjax呼び出しが起動します。

于 2010-12-30T17:23:14.487 に答える
0

コントローラーに以下のアクションがありますか

public ActionResult Add()
{
return View( "GiftEditorRow"、new MyList());
}

于 2010-12-25T13:00:40.363 に答える