MVC"创建视图"当有一个模型一对多的关系 [英] MVC "create view" when there is one to many relationship in model
问题描述
我的模型很简单,一个客户端可以有多个电话号码:
My model is simple, one client can have many phone numbers :
我已经重新$ P $实体框架psented这个
I have represented this in Entity Framework
生成的客户端类如下。
public partial class Client
{
public Client()
{
this.PhoneNumbers = new HashSet<PhoneNumber>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; }
}
现在我需要为创造客户创建视图页面。此页面应该有空间也进入PHONENUMBERS(例如:默认情况下应该有两个文本框中输入电话号码)
And now I need to create a view page for "create client". This page should have space to enter PhoneNumbers also (ex: By default there should be two text boxes to enter phone numbers)
<fieldset>
<legend>Client</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
为上述的CREATE VIEW,我们可以轻易放弃的空间model.Name,因为它是一个简单的属性。但是,我该怎么办的电话号码收集类似的东西??
as the above "create view" we can easily give an space for "model.Name", because it is a simple property. But how can i do something similar for collection of phone numbers..??
我知道,我们可以用丑陋的JavaScript code ++实现这一点,但我想知道最好的容易和简单的方式,我们可以在ASP.NET MVC ...?
I know that we can achieve this with ugly javascript code, but I would like to know the best easy and simple way, that we can use with ASP.NET MVC ... ?
推荐答案
您所要做的几件事情:
首先创建一个视图模型
有你需要的属性:
First create a ViewModel
that has the properties you need:
public class ClientViewModel
{
public int Id {get;set;}
public string Name {get;set;}
public PhoneNumber PhoneNumber1 {get;set;}
public PhoneNumber PhoneNumber2 {get;set;}
}
修改创建
返回 ClientViewModel
[HttpGet]
public ActionResult Create()
{
return View(new ClientViewModel());
}
映射 HttpPost
使用 ClientViewModel
和值映射到它:
[HttpPost]
public ActionResult Create(ClientViewModel clientViewModel)
{
var client = new Client();
client.Name = clientViewModel.Name;
client.PhoneNumbers.Add(clientViewModel.PhoneNumber1);
client.PhoneNumbers.Add(clientViewModel.PhoneNumber2);
db.Clients.Add(client);
db.SaveChanges();
return RedirectToAction("Index", "Client");
}
然后,最后,修改你的看法:
Then, finally, modify your view:
<fieldset>
<legend>Client</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PhoneNumber1.Number)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PhoneNumber1.Number)
@Html.ValidationMessageFor(model => model.PhoneNumber1.Number)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PhoneNumber2.Number)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PhoneNumber2.Number)
@Html.ValidationMessageFor(model => model.PhoneNumber2.Number)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
这篇关于MVC&QUOT;创建视图&QUOT;当有一个模型一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!