将通用列表返回给 MVC 控制器
我有一个看起来像这样的课程;
public class item
{
public string Tradingname { get; set; }
}
我有一个继承自“item”类的部分视图;
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<item>" %>
<%= Html.TextBoxFor(x => x.Tradingname) %>
在我看来,我创造了其中的一些。让我们说2;
<% using (Html.BeginForm())
{ %>
<% Html.RenderPartial("TradingName", new Binding.Models.item()); %><br />
<% Html.RenderPartial("TradingName", new Binding.Models.item()); %><br />
<input type="submit" />
<%} %>
然后在我的控制器中我希望能够写这个;
[HttpPost]
public ActionResult Index(List<item> items)
或者
[HttpPost]
public ActionResult Index([Bind(Prefix = "Tradingname")]List<item> items)
但我似乎无法将任何数据从我的部分视图返回到我的列表中。有人知道如何从一组可变的partialViews 中获取可变的数据列表吗?
I have a class that looks like this;
public class item
{
public string Tradingname { get; set; }
}
I have a Partial View that inherits from the "item" class;
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<item>" %>
<%= Html.TextBoxFor(x => x.Tradingname) %>
In my view I create a number of them. Let's Say 2;
<% using (Html.BeginForm())
{ %>
<% Html.RenderPartial("TradingName", new Binding.Models.item()); %><br />
<% Html.RenderPartial("TradingName", new Binding.Models.item()); %><br />
<input type="submit" />
<%} %>
Then in my controller I was hoping to be able to write this;
[HttpPost]
public ActionResult Index(List<item> items)
or
[HttpPost]
public ActionResult Index([Bind(Prefix = "Tradingname")]List<item> items)
But I can't seem to get any data back from my partial views into my List. Anyone know how I can get a variable list of data back from a variable set of partialViews?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您使用编辑器模板:
Controller:
和 in the view:
最后只需将部分移动到
~/Views/Home/EditorTemplates/item.ascx
(名称和位置很重要):I would recommend you using editor templates:
Controller:
and in the view:
and finally simply move the partial in
~/Views/Home/EditorTemplates/item.ascx
(name and location are important):