将通用列表返回给MVC控制器

时间:2011-04-05 04:43:27

标签: asp.net-mvc model-view-controller

我有一个看起来像这样的课程;

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中获取变量数据列表?

1 个答案:

答案 0 :(得分:3)

我建议您使用编辑器模板:

控制器:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        List<item> model = ...
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(List<item> items)
    {
        ...
    }
}

并在视图中:

<% using (Html.BeginForm()) { %>
    <%= Html.EditorForModel();
    <input type="submit" />
<% } %>

最后只需移动~/Views/Home/EditorTemplates/item.ascx中的部分(名称和位置很重要):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<item>" %>
<%= Html.TextBoxFor(x => x.Tradingname) %><br/>
相关问题