我想在我的发票页面上有 2 个下拉菜单,其模型如下:
public class Invoice {
[Key]
public int InvoiceID { get; set; }
public int MemberID { get; set; }
public int AddressID { get; set; }
public virtual Member Member { get; set; }
public virtual MembersAddress MembersAddress { get; set; }
public virtual ICollection<InvoiceLineItem> InvoiceLineItems { get; set; }
}
虽然向导为我和代码自动生成了 CRUD 页面,但是当我从第一个下拉列表中选择成员名称时,我只需要填充该成员的地址。InvoiceController 中 Create() 方法的代码是:
public ActionResult Create()
{
ViewBag.MemberID = new SelectList(db.Members, "MemberID", "FirstName");
return View();
}
cshtml文件是:
@model MvcDemoApp.Models.Invoice
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Invoice</legend>
<div class="editor-label">
@Html.LabelFor(model => model.MemberID, "Member")
</div>
<div class="editor-field">
@Html.DropDownList("MemberID", String.Empty)
@Html.ValidationMessageFor(model => model.MemberID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AddressID)
</div>
<div class="editor-field">
@@Html.EditorFor(model => model.AddressID)@
@Html.ValidationMessageFor(model => model.AddressID)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
提前致谢。