返回泛型列表的MVC控制器 [英] Return a generic list to the MVC Controller

查看:101
本文介绍了返回泛型列表的MVC控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,看起来像这样;

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

在我看来,我创建了一些人。比方说,2;

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)

    [HttpPost]
    public ActionResult Index([Bind(Prefix = "Tradingname")]List<item> items)

但我似乎无法得到任何数据从我的局部视图返回到我的列表。任何人都知道我如何从一个变量组partialViews的获取数据的变量列表回来?

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?

推荐答案

我使用编辑器模板会建议你:

I would recommend you using editor templates:

控制器:

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" />
<% } %>

和最后索性搬进〜/查看/主页/ EditorTemplates / item.ascx (名称和位置是很重要的)部分:

and finally simply move the partial in ~/Views/Home/EditorTemplates/item.ascx (name and location are important):

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

这篇关于返回泛型列表的MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆