Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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
Asp.net mvc MVC4创建一个表单,将列表作为我的新对象的属性_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc MVC4创建一个表单,将列表作为我的新对象的属性

Asp.net mvc MVC4创建一个表单,将列表作为我的新对象的属性,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我正在为注册新用户创建一个表单,每个用户都可以有多个地址。(一个用户有多个地址)。我找到了这篇文章,但并不适合我,因为我有一个主要属性,即user,地址必须与之相关。我想为添加多个地址创建一些类似的东西,即使在为新用户创建屏幕中也不存在,因为这些地址的用户主键是相关的 有人能举个例子吗?更好的方法是什么 这与我的代码非常接近: public class Person{ public int PersonId { get; set; } public string Name { ge

我正在为注册新用户创建一个表单,每个用户都可以有多个地址。(一个用户有多个地址)。我找到了这篇文章,但并不适合我,因为我有一个主要属性,即user,地址必须与之相关。我想为添加多个地址创建一些类似的东西,即使在为新用户创建屏幕中也不存在,因为这些地址的用户主键是相关的

有人能举个例子吗?更好的方法是什么

这与我的代码非常接近:

public class Person{
   public int PersonId { get; set; }
   public string Name { get; set; }      
   public virtual ICollection<Address> Addresses { get; set; }
}    
public class Address{
   public int AddressId { get; set; }
   public string Street { get; set; }      

   [ScriptIgnore]
   public virtual Person Person { get; set; }
}
公共类人物{
公共int PersonId{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection地址{get;set;}
}    
公共课堂演讲{
public int AddressId{get;set;}
公共字符串Street{get;set;}
[脚本忽略]
公共虚拟人{get;set;}
}
类似的内容可能更接近您的需要。您需要设想复选框是用于输入地址的字段集合

但是,如果为Person创建的ViewModel包含Address的ViewModels列表,您可以创建与ViewModels同名的强类型编辑器模板和显示模板,如果您使用
@Html.EditorFor
@Html.DisplayFor
,则会自动拾取这些模板,从而简化一对多操作

如果你有这样的档案-

~/Models/PersonViewModel.cs
~/Models/AddressViewModel.cs
~/Views/Person/Edit.cshtml
~/Views/Shared/DisplayTemplates/AddressViewModel.cshtml
~/Views/Shared/EditorTemplates/AddressViewModel.cshtml
还有一个有点像

public class PersonViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    ...
    public List<AddressViewModel> Addresses { get; set; }
}
公共类PersonViewModel
{
公共int Id{get;set;}
公共字符串名称{get;set;}
...
公共列表地址{get;set;}
}
如果你有一个像这样的人的编辑视图

@model PersonViewModel

<div>
@using (Html.BeginForm("Edit", "Person", new {id = Model.Id}, FormMethod.Post))
{
    <div>
        @Html.EditorForModel()
    </div>
    <div>
        <p>@Html.DisplayNameFor(p => p.Addresses)</p>

        @Html.EditorFor(p => p.Addresses)
    </div>

    <p>
        <input type="submit" value="Save"/>
    </p>
}
</div>
@model PersonViewModel
@使用(Html.BeginForm(“Edit”,“Person”,new{id=Model.id},FormMethod.Post))
{
@Html.EditorForModel()
@DisplayNameFor(p=>p.Addresses)

@EditorFor(p=>p.Addresses)

}
然后,对于列表中的每个条目,应该为AddressViewModel选择一次编辑器模板。您必须添加一些Ajax,以允许像示例链接中那样创建新地址。只要模板包含AddressViewModel要工作的所有字段,那么您的编辑帖子控制器应该只接收PersonViewModel作为其参数返回

我的示例中缺少一些部分,但您应该能够填补教程中的空白。

类似的内容可能更接近您需要的内容。您需要设想复选框是用于输入地址的字段集合

但是,如果为Person创建的ViewModel包含Address的ViewModels列表,您可以创建与ViewModels同名的强类型编辑器模板和显示模板,如果您使用
@Html.EditorFor
@Html.DisplayFor
,则会自动拾取这些模板,从而简化一对多操作

如果你有这样的档案-

~/Models/PersonViewModel.cs
~/Models/AddressViewModel.cs
~/Views/Person/Edit.cshtml
~/Views/Shared/DisplayTemplates/AddressViewModel.cshtml
~/Views/Shared/EditorTemplates/AddressViewModel.cshtml
还有一个有点像

public class PersonViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    ...
    public List<AddressViewModel> Addresses { get; set; }
}
公共类PersonViewModel
{
公共int Id{get;set;}
公共字符串名称{get;set;}
...
公共列表地址{get;set;}
}
如果你有一个像这样的人的编辑视图

@model PersonViewModel

<div>
@using (Html.BeginForm("Edit", "Person", new {id = Model.Id}, FormMethod.Post))
{
    <div>
        @Html.EditorForModel()
    </div>
    <div>
        <p>@Html.DisplayNameFor(p => p.Addresses)</p>

        @Html.EditorFor(p => p.Addresses)
    </div>

    <p>
        <input type="submit" value="Save"/>
    </p>
}
</div>
@model PersonViewModel
@使用(Html.BeginForm(“Edit”,“Person”,new{id=Model.id},FormMethod.Post))
{
@Html.EditorForModel()
@DisplayNameFor(p=>p.Addresses)

@EditorFor(p=>p.Addresses)

}
然后,对于列表中的每个条目,应该为AddressViewModel选择一次编辑器模板。您必须添加一些Ajax,以允许像示例链接中那样创建新地址。只要模板包含AddressViewModel要工作的所有字段,那么您的编辑帖子控制器应该只接收PersonViewModel作为其参数返回

我的示例中缺少一些部分,但您应该能够填补教程中的空白。

类似的内容可能更接近您需要的内容。您需要设想复选框是用于输入地址的字段集合

但是,如果为Person创建的ViewModel包含Address的ViewModels列表,您可以创建与ViewModels同名的强类型编辑器模板和显示模板,如果您使用
@Html.EditorFor
@Html.DisplayFor
,则会自动拾取这些模板,从而简化一对多操作

如果你有这样的档案-

~/Models/PersonViewModel.cs
~/Models/AddressViewModel.cs
~/Views/Person/Edit.cshtml
~/Views/Shared/DisplayTemplates/AddressViewModel.cshtml
~/Views/Shared/EditorTemplates/AddressViewModel.cshtml
还有一个有点像

public class PersonViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    ...
    public List<AddressViewModel> Addresses { get; set; }
}
公共类PersonViewModel
{
公共int Id{get;set;}
公共字符串名称{get;set;}
...
公共列表地址{get;set;}
}
如果你有一个像这样的人的编辑视图

@model PersonViewModel

<div>
@using (Html.BeginForm("Edit", "Person", new {id = Model.Id}, FormMethod.Post))
{
    <div>
        @Html.EditorForModel()
    </div>
    <div>
        <p>@Html.DisplayNameFor(p => p.Addresses)</p>

        @Html.EditorFor(p => p.Addresses)
    </div>

    <p>
        <input type="submit" value="Save"/>
    </p>
}
</div>
@model PersonViewModel
@使用(Html.BeginForm(“Edit”,“Person”,new{id=Model.id},FormMethod.Post))
{
@Html.EditorForModel()
@DisplayNameFor(p=>p.Addresses)

@EditorFor(p=>p.Addresses)

}
然后,对于列表中的每个条目,应该为AddressViewModel选择一次编辑器模板。您必须添加一些Ajax,以允许像示例链接中那样创建新地址。只要模板包含AddressViewModel要工作的所有字段,那么您的编辑帖子控制器应该只接收PersonViewModel作为其参数返回

我的示例中缺少一些部分,但您应该能够填补教程中的空白。

类似的内容可能更接近您需要的内容。您需要设想复选框是用于输入地址的字段集合

但是,如果为Person创建的ViewModel包含Address的ViewModels列表,则可以使用c