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

发布于 2024-10-30 20:50:59 字数 956 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

面犯桃花 2024-11-06 20:50:59

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

Controller:

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

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

和 in the view:

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

I would recommend you using editor templates:

Controller:

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

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

and in the view:

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

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/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文