【发布时间】:2011-05-28 00:31:27
【问题描述】:
我正在尝试向我的 mvc 应用程序添加动态内容。
我正在使用 Steven Sanderson 的帖子 Editing a variable length list, ASP.NET MVC 2-style,多亏了它,我在其中添加了一些动态内容。 由于我的应用程序的一些限制,我不得不对基础 Gift 类进行更改。
问题是现在应用程序无法工作。
在我的模型中:
public class Gift
{
//public string Name { get; set; }
//public double Price { get; set; }
public List<string> MyList { get; set; }
}
现在,我在礼物参数中得到了空值
[HttpPost]
public ActionResult Index(IEnumerable<Gift> gifts)
{
return View("Completed", gifts);
}
谁能帮我解决这个问题?
编辑: 这是我的查看代码:
<%@ 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>
编辑2 我还从 Phil Haack 找到了 Model Binding To A List 的帖子,并且工作正常,但我无法实现“添加图书”功能!!!
【问题讨论】:
-
Sanderson 博客中的方式不能直接工作,因为 mvc 不能直接绑定列表类型对象。您也可以在这里显示您的视图代码吗?
-
@xandy:你确定当你说MVC不能直接bind the List type对象???
-
我的意思是,您不能简单地将其视为一个变量并让 MVC 神奇地绑定。给出一个索引是解决方案,但是如果涉及到另一个对象下的列表,一个简单的索引是不够的,自定义绑定行为(在控制器中)或给出一个命名的索引,如 myList[0] 是必要的。
-
@xandy:没问题。我只是想指出,您的第一条评论可能会导致误解。 @muek:正如 xandy 已经写的那样,您应该添加一些视图代码以获得更多帮助...
-
@xandy 我添加了我的视图代码
标签: jquery asp.net-mvc asp.net-mvc-2 dynamic-content