在MVC中添加动态表单内容
我正在尝试将动态内容添加到我的 mvc 应用程序中。
我正在使用 Steven Sanderson 帖子 编辑可变长度列表,ASP.NET MVC 2 风格,多亏了它,我在其中添加了一些动态内容。 由于我的应用程序的一些限制,我不得不对基本 Gift 类进行更改。
问题是现在该应用程序无法运行。
在我的模型中,我有:
public class Gift
{
//public string Name { get; set; }
//public double Price { get; set; }
public List<string> MyList { get; set; }
}
现在,我在gifts参数中得到空值
[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>
Edit2 我还发现了帖子 Model Binding To A来自 Phil Haack 的列表,工作得很好,但我无法实现“添加图书”功能!
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' !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您正在循环访问模型,而不是模型中的项目。
您可以执行以下操作:
我们假设您的部分视图仅接收一个字符串。
至于您的 Ajax 调用不起作用,问题在于您尝试提交两次,一次使用 actionLink,另一次使用 Ajax。将 ActionLink 更改为显式链接,后面没有任何操作:
然后您的 jQuery Ajax 调用将触发。
The problem is that you're looping through the model and not the items in the model.
Here's what you can do:
We'll assume that your partial view receives only a string.
As far as your Ajax call not working, the problem there is that you're trying to submit twice, once using the actionLink and the other using Ajax. Change the ActionLink to an explicit link with no action behind it:
Then your jQuery Ajax call will fire.
您的控制器中是否有以下操作
public ActionResult Add()
{
return View("GiftEditorRow", new MyList());
}
Do you have below action into your controllor
public ActionResult Add()
{
return View("GiftEditorRow", new MyList());
}