Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多实体模型绑定_C#_Asp.net Mvc - Fatal编程技术网

C# 多实体模型绑定

C# 多实体模型绑定,c#,asp.net-mvc,C#,Asp.net Mvc,我正在开发一个管理面板,允许用户添加一个或多个其他用户。有一个文本区域,管理员可以在其中输入一个或多个要添加到应用程序的用户ID,该ID对应于在雇用过程中分配的用户ID。提交时,应用程序从包含所有员工的数据库中提取姓名、电子邮件等,并将其显示在屏幕上进行验证。屏幕上还包括一些用于分配某些权限的复选框,例如CanWrite和IsAdmin 查看 using (Html.BeginForm()) { <table>

我正在开发一个管理面板,允许用户添加一个或多个其他用户。有一个文本区域,管理员可以在其中输入一个或多个要添加到应用程序的用户ID,该ID对应于在雇用过程中分配的用户ID。提交时,应用程序从包含所有员工的数据库中提取姓名、电子邮件等,并将其显示在屏幕上进行验证。屏幕上还包括一些用于分配某些权限的复选框,例如
CanWrite
IsAdmin

查看

using (Html.BeginForm())
        {
            <table>
                <tr>
                    <th>
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.User.First().ID)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.User.First().Name)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.User.First().Email)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.User.First().CanWrite)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.User.First().IsAdmin)
                    </th>
                </tr>

                @foreach (var item in Model.User)
                {
                    <tr>
                        <td>
                            <input type="checkbox" name="id" value="@item.ID" checked=checked/>
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.ID)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Name)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Email)
                        </td>
                        <td>
                            @Html.CheckBoxFor(modelItem => item.CanWrite)
                        </td>
                        <td>
                            @Html.CheckBoxFor(modelItem => item.IsAdmin)
                        </td>
                    </tr>
                }

            </table>
            <input type="submit" />
        }
@model Employee

@using( Html.BeginForm()
{
    // build HTML table, not much different from yours
}
控制器

[HttpPost]
public ActionResult Create(IEnumerable<User> model)
{
    //With this code, model shows up as null
}
[HttpPost]
public ActionResult Create(Employee model)
{
    // model will have an IEnumerable property with the User info
}
[HttpPost]
公共行动结果创建(IEnumerable模型)
{
//使用此代码,模型显示为null
}

对于单个用户,我知道我可以使用
user model
作为控制器操作中的参数。如何调整此代码,使其能够同时添加多个用户?这可能吗?

我只是开始学习ASP.NET MVC,但我想您只需要创建一个视图模型类,该类的属性类型为
IEnumerable
,用于存储用户集合。然后将其传递到视图并返回到处理程序。

尝试以下操作:

<input checked="checked" data-val="true" data-val-required="The CanWrite field is required." name="[0].CanWrite" type="checkbox" value="true" />
<input name="[0].CanWrite" type="hidden" value="false" />
<input checked="checked" data-val="true" data-val-required="The CanWrite field is required." name="[1].CanWrite" type="checkbox" value="true" />
<input name="[1].CanWrite" type="hidden" value="false" />
型号

public class User
{
    public int ID { set; get; }
    public string Name { set; get; }
    public bool IsAdmin { set; get; }
    public bool CanWrite { set; get; }
    public string Email{ set; get; }

}
public class Employee
{
    public IEnumerable<User> Employees { set; get; }
}

public class User
{
    public int ID { set; get; }
    public string Name { set; get; }
    public bool IsAdmin { set; get; }
    public bool CanWrite { set; get; }
    public string Email{ set; get; }
}
控制器

[HttpPost]
public ActionResult Create(IEnumerable<User> model)
{
    //With this code, model shows up as null
}
[HttpPost]
public ActionResult Create(Employee model)
{
    // model will have an IEnumerable property with the User info
}

你走对了,杰夫。你只需要扩展一下你所拥有的

首先,模型在操作中为
null
的原因是表单上输入字段的命名方式。您的字段将按照以下方式渲染:

<input id="item_CanWrite" name="item.CanWrite" type="checkbox" value="true" />
<input name="item.CanWrite" type="hidden" value="false" />
如果您现在检查这些字段是如何呈现为HTML的,您将看到它们的外观如下:

<input checked="checked" data-val="true" data-val-required="The CanWrite field is required." name="[0].CanWrite" type="checkbox" value="true" />
<input name="[0].CanWrite" type="hidden" value="false" />
<input checked="checked" data-val="true" data-val-required="The CanWrite field is required." name="[1].CanWrite" type="checkbox" value="true" />
<input name="[1].CanWrite" type="hidden" value="false" />
然而,我想在此提出一个进一步的改进,以消除您认为的逻辑。为此,我们可以使用
DisplayTemplates
而不是循环:

  • 在视图的当前文件夹中创建一个
    DisplayTemplates
    文件夹(例如,如果视图是
    Home\Index.cshtml
    ,则创建文件夹
    Home\DisplayTemplates
  • 在该目录中创建一个强类型视图,其名称与您的模型匹配(即,在本例中,该视图将被称为
    User.cshtml
  • 在视图中放置重复的逻辑
  • 您的模板现在应该是这样的(除了
    用户
    模型所在位置的名称空间):

    @模型用户
    @DisplayFor(m=>m.ID)
    @DisplayFor(m=>m.Name)
    @DisplayFor(m=>m.Email)
    @CheckBoxFor(m=>m.CanWrite)
    @CheckBoxFor(m=>m.IsAdmin)
    
    现在,在原始视图中,可以使用以下内容替换所有循环:

    @using (Html.BeginForm())
    {
        <table>
            @Html.DisplayForModel()
        </table>
        <input type="submit" value="Submit" />
    }
    
    @使用(Html.BeginForm())
    {
    @Html.DisplayForModel()
    }
    

    如果您想了解有关集合绑定的更多信息,请参阅我提供的另一个问题。

    正是我的意思。很好的例子。