【发布时间】:2015-11-16 10:18:57
【问题描述】:
我是学习 MVC 的新手,非常感谢您的帮助。我有两个模型,公司和地址。我试图有一个简单的表格,有人可以在其中添加公司详细信息。
加载公司表单时,会呈现包含地址模型的编辑器模板。我遇到的问题是直截了当的。我什至在堆栈溢出时看到了解决方案,但在我的情况下它不会工作(奇怪)。当我将新的Innuendo.Models.AddressModel() 传递给地址模板时,在回发期间ModelState.IsValid 为假,Model.Address 仍设置为空。我究竟做错了什么?
公司观点
@model Innuendo.Models.FirmModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>FirmModel</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>
@Html.Partial("~/Views/shared/EditorTemplates/_Address.cshtml", new Innuendo.Models.AddressModel())
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LogoPath)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LogoPath)
@Html.ValidationMessageFor(model => model.LogoPath)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
编辑器模板_地址
@model Innuendo.Models.AddressModel
<div class="editor-label">
@Html.LabelFor(model => model.HouseName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.HouseName)
@Html.ValidationMessageFor(model => model.HouseName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Street)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Street)
@Html.ValidationMessageFor(model => model.Street)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Town)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Town)
@Html.ValidationMessageFor(model => model.Town)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.County)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.County)
@Html.ValidationMessageFor(model => model.County)
</div>
财务总监
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(FirmModel firmmodel)
{
if (ModelState.IsValid)
{
firmmodel.FirmId = Guid.NewGuid();
db.Firms.Add(firmmodel);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(firmmodel);
}
企业模型
[Table("Firms")]
public class FirmModel
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public Guid FirmId { get; set; }
[Display(Name = "Firm Name")]
[Required]
[StringLength(250)]
public string Name { get; set; }
[Required]
public virtual AddressModel Address { get; set; }
[StringLength(250)]
public string LogoPath { get; set; }
}
地址模型
[Table("Addresses")]
public class AddressModel
{
[Key]
[HiddenInput(DisplayValue = false)]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public Guid AddressId { get; set; }
[Required]
[Display(Name = "House Name/Number")]
[StringLength(250)]
public string HouseName { get; set; }
[StringLength(250)]
[Required]
public string Street { get; set; }
[StringLength(250)]
[Required]
public string Town { get; set; }
[StringLength(250)]
[Required]
public string County { get; set; }
}
【问题讨论】:
标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 razor