我有以下模型图,其中Address实体依赖于Profile实体。
我创建了一个创建视图:
@using (Html.BeginForm())
{
<h4>Profile</h4>
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
<br />
@Html.LabelFor(model => model.Address.City, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Address.City, new { htmlAttributes = new { @class = "form-control" } })
<br />
<input type="submit" value="Create" class="btn btn-default" />
}
这就是EF为我搭建的行动:
public ActionResult Create(Profile profile)
{
if (ModelState.IsValid)
{
db.Profiles.Add(profile);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(profile);
}
我如何实现我想达到的目标。 提前谢谢。