我正在尝试使用ASP.NET MVC中的默认模型活页夹来绑定隐藏字段的列表

<input id="entity" name="entity" type="hidden" value="/string/one/here" />
<input id="entity" name="entity" type="hidden" value="/another/string/here" />
<input id="entity" name="entity" type="hidden" value="/last/string/here" />

List<string> 像这样:

    [HttpPost]
    public ActionResult Move(List<string> entity)
    {
        return View(entity);
    }

当我发布表单时,视图显示这样的列表内容:

        <%foreach (string item in Model)
          {%>

          <%: Html.Hidden("entity", item)%>
          <!--binding not working correctly -->

        <%} %>

生成的内容是:

<input id="entity" name="entity" type="hidden" value="/last/string/here" />
<input id="entity" name="entity" type="hidden" value="/last/string/here" />
<input id="entity" name="entity" type="hidden" value="/last/string/here" />

我很混乱。我尝试将初始隐藏字段更改为:

<input id="entity__" name="entity[]" type="hidden" value="/string/one/here" />
<input id="entity__" name="entity[]" type="hidden" value="/another/string/here" />
<input id="entity__" name="entity[]" type="hidden" value="/last/string/here" />

但是,当我尝试发布我有一个错误时:

 System.NullReferenceException: Object reference not set to an instance of an object.

在此行中的这一行中:

<%foreach (string item in Model)

我已经能够在没有名称中使用索引之前绑定字符串列表。我不明白为什么这不能正常工作。也许新鲜的眼睛可以帮助我解决这个问题?

谢谢

编辑

继续过期。我抛弃了htmlhelper,并在内部进行了硬编码 foreach 这样的循环:

<%foreach (string item in Model)
  {%>
      <input id="entity" name="entity" type="hidden" value="<%:item %>" />
<%} %>

这个 工作. 。我不知道HTML助手的代码有什么区别,因此我并排放置两者并试图将它们匹配。

<input id="entity" name="entity" type="hidden" value="//Content/files/NewDirectory/Aesculuparviflora011cm.jpg" />
<input id="entity" name="entity" type="hidden" value="//Content/files/NewDirectory/Aesculuparviflora011cm.jpg" />

他们是相同的。但是,当我使用硬码标签时,绑定起作用,当我使用HTML助手时,它不起作用。是什么赋予了?????????

有帮助吗?

解决方案

您为什么要在强烈打字的视图中使用循环和非紧张的助手?编辑模板可能会如此简单:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new[] 
        {
            "/string/one/here", "/another/string/here", "/last/string/here"    
        });
    }

    [HttpPost]
    public ActionResult Index(List<string> items)
    {
        return View(items);
    }
}

和相应的视图(~/Views/Home/Index.aspx):

<%@ Page 
    Title="" 
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<IEnumerable<string>>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% using (Html.BeginForm()) { %>
        <%: Html.EditorForModel() %>
        <input type="submit" value="OK" />
    <% } %>
</asp:Content>

以及相应的编辑器模板(~/Views/Home/Index/EditorTemplates/string.ascx):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%: Html.HiddenFor(x => x) %>

现在,您不再需要担心价值不正确的绑定,在您的视图中编写循环,...您最终可以专注于应用程序的真实业务逻辑。

其他提示

尝试:

<input id="entity" name="entity0" type="hidden" value="/last/string/here" /> 
<input id="entity" name="entity1" type="hidden" value="/last/string/here" /> 
<input id="entity" name="entity2" type="hidden" value="/last/string/here" /> 

实际上,我无法重新创建您的问题:这是我的代码,我尝试摆弄它,但我无法使其发生故障。也许检查以使您的视图继承正确的类型。请发布您所有的代码坏死,以使其故障。

    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";

        var strings = new string[] { "test1", "test2", "test3" };

        return View(strings);
    }

    [HttpPost]
    public ActionResult Index(List<string> strings)
    {

        return View(strings);
    }

ASPX页面

       <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<string>>" %>
        <%using (Html.BeginForm()) { %>

        <% foreach (var item in Model) { %>

            <%:Html.Hidden("strings", item)%>   

        <% } %>

        <input type="submit" value="Submit" />
    <% } %>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top