<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ApexAdmin.Master"
Inherits="System.Web.Mvc.ViewPage<ApexTrackDays.Models.Event>" %>
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true) %>
<tr><td>Experience level</td><td><div class="editor-field">
<%: Html.DropDownListFor(model => model.ExperienceLevelID, (SelectList)ViewData["Experience"], "--select--")%>
<%: Html.ValidationMessageFor(model => model.ExperienceLevelID) %>
</div></td></tr>
<tr><td>No of Helmets</td><td><div class="editor-field">
<%: Html.DropDownList("helmets", (SelectList)ViewData["size"], "--select--")%>
</div></td></tr>
<tr><td>No of Garages</td><td><div class="editor-field">
<%: Html.DropDownList("garages", (SelectList)ViewData["garages"], "--select--")%>
</div></td></tr>
[HttpPost]
public ActionResult Create(Event trackday)
{
ViewBag.mode = "create";
// for dropdown experience -- Inserts into Events table
IExperienceLevelRepository expLevelResp = new ExperienceLevelRepository();
IQueryable<ExperienceLevel> expLevel = expLevelResp.GetAllExperienceLevels().OrderBy(ExperienceLevel => ExperienceLevel.Name);
ViewData["Experience"] = new SelectList(expLevel, "ID", "Name");
// dropdown for helmets -- This should insert into Products table
var helmets = Enumerable.Range(1, 200).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
ViewData["helmets"] = new SelectList(helmets.ToList(), "Value", "Text");
// dropdown for garages -- This should insert into Products table
var garages = Enumerable.Range(1, 50).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
ViewData["garages"] = new SelectList(garages.ToList(), "Value", "Text");
if (ModelState.IsValid)
{
trackday.DateAdded = DateTime.Now; //Inserts Date into Events table
trackday.DateModified = DateTime.Now; //Inserts Date into Events table
trackdayResp.Add(trackday);
trackdayResp.Save();
return RedirectToAction("Index");
}
else
{
return View();
}
}
嗨,我与事件有一对多的关系(1):产品(*)[1事件可以有不同的产品可用],但是当使用EF插入数据时,它只插入到事件表中,它不会级联插入有产品表,任何想法或任何帮助将受到高度赞赏。感谢名单
答案 0 :(得分:0)
您的产品视图中没有任何内容 尝试添加一行
<%: Html.EditorFor(model=>mode.Products) %>
将为您的产品呈现编辑器 - 但您可能希望通过在EditorTemplates中创建Products.ascx来定制它?
希望这会有所帮助。