我有一个创建页面,用于为用户添加节日和页面,以便为该节日创建活动。我试图传递节日ID并将其存储在我的事件表中。它被捕获在创建页面的帖子后面。这是代码:
控制器:
[HttpGet]
public ActionResult Create2(int festID)
{
EventsVM events = new EventsVM { festivalID = festID };
events.eType = db.EType.ToDictionary(p => p.ID, q => q.EType);
events.eType.Add(-1, "----- Add New Event Type -----");
events.eventsDate = DateTime.Now;
events.startTime = DateTime.Now;
events.endTime = DateTime.Now;
return View(events);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create2(EventsVM model)
{
if (ModelState.IsValid != true)
{
if (model.selectedEType != -1)
{
//db.save stuff from create.
Events Newevent = new Events();
Newevent.EndTime = model.endTime;
Newevent.StartTime = model.startTime;
Newevent.EventsDate = model.eventsDate = DateTime.Now;
Newevent.EventsName = model.EventsName;
Newevent.EType = db.EType.Where(p => p.ID == model.selectedEType).Single();
Newevent.Location = model.Location;
//Caught here!!!
Newevent.FestivalID = model.festivalID;
db.Events.Add(Newevent);
db.SaveChanges();
return RedirectToAction("Details", "Festival", new { id = model.festivalID });
//return RedirectToAction("Index", "Festival");
//return RedirectToAction("Index", "Events");
//String test = "test3";
}
ModelState.AddModelError("", "No Event Type Picked");
}
model.eType = db.EType.ToDictionary(p => p.ID, q => q.EType);
model.eType.Add(-1, "----- Add New Event Type -----");
model.eventsDate = DateTime.Now;
model.startTime = DateTime.Now;
model.endTime = DateTime.Now;
return View(model);
}
我必须在所选节日ID
的详细信息页面上“保存”节日IDDetails.cshtml - 节日
<div>
<h3>Details for Festival: @Model.FestivalName.ToString()</h3>
</div>
<hr />
<div class="table-responsive">
<!--<div class="table">-->
<table class="table">
<tr>
<th>
Festival Name
</th>
<th>
Start Date
</th>
<th>
End Date
</th>
<th>
Town
</th>
<th>
County
</th>
<th>
Festival Type
</th>
<th>
Options
</th>
</tr>
<tr>
<td>
@Html.DisplayFor(model => model.FestivalName)
</td>
<td>
@Html.DisplayFor(model => model.StartDate)
</td>
<td>
@Html.DisplayFor(model => model.EndDate)
</td>
<td>
@Html.DisplayFor(model => model.FestivalTown.Name)
</td>
<td>
@Html.DisplayFor(model => model.FestivalCounty.Name)
</td>
<td>
@Html.DisplayFor(model => model.FType.FType)
</td>
<td>
<div class="btn-group">
@* <button class="btn btn-info btn-sm dropdown-toggle" type="button" data-toggle="dropdown">Options</button>*@
<button type="button" class="btn btn-info btn-sm dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li role="presentation" class="dropdown-header">Festival</li>
<li>@Html.ActionLink("Edit", "Edit2", new { id = Model.FestivalId })</li>
<li>@Html.ActionLink("Back to List", "Index")</li>
</ul>
</div>
</td>
</tr>
</table>
</div>
@if (Model.Events != null)
{
<center><div class="well well-sm" style="width:400px">@Model.FestivalName.ToString() has @Model.Events.Count() Events</div></center>
@Html.ActionLink("Add Event", "Create2", "Events", new { festID = Model.FestivalId }, new { @class = "btn btn-primary"})
<br />
<br />
<!--<div class="table-responsive">-->
<div class="table">
<table class="table table-striped">
<tr>
<th>Events Name
</th>
<th>Events Date
</th>
<th>Start Time
</th>
<th>End Time
</th>
<th>Options
</th>
</tr>
@foreach (var e in Model.Events)
{
<tr>
<td>
@Html.DisplayFor(modelItem => e.EventsName)
</td>
<td>
@Html.DisplayFor(modelItem => e.EventsDate)
</td>
<td>
@Html.DisplayFor(modelItem => e.StartTime)
</td>
<td>
@Html.DisplayFor(modelItem => e.EndTime)
</td>
<td>
<div class="btn-group">
<button type="button" class="btn btn-success btn-sm dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li role="presentation" class="dropdown-header">Event</li>
<li role="presentation" class="divider"></li>
<li>@Html.ActionLink("Edit", "Edit2", "Events", new { id = e.ID }, null)</li>
<li>@Html.ActionLink("Details", "Details", "Events", new { id = e.ID }, null)</li>
<li>@Html.ActionLink("Delete", "Delete", "Events", new { id = e.ID }, null)</li>
</ul>
</div>
</td>
</tr>
}
</table>
</div>
}
我得到的错误是当它回发时,链接中的id变为0,因此它不保存id。关于如何解决这个问题的任何ID?
答案 0 :(得分:0)
id为0,因为您传递的是model.ID而不是EF在SaveChanges之后生成的实体ID(将更改传播到数据库中)并添加到ObjectSet。因此,ID会自动为您填写。将您的代码更改为此。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create2(EventsVM model)
{
if (ModelState.IsValid != true)
{
if (model.selectedEType != -1)
{
//db.save stuff from create.
Events Newevent = new Events();
Newevent.EndTime = model.endTime;
Newevent.StartTime = model.startTime;
Newevent.EventsDate = model.eventsDate = DateTime.Now;
Newevent.EventsName = model.EventsName;
Newevent.EType = db.EType.Where(p => p.ID == model.selectedEType).Single();
Newevent.Location = model.Location;
//Caught here!!!
Newevent.FestivalID = model.festivalID;
db.Events.Add(Newevent);
db.SaveChanges();
//Change the model.festivalID to Newevent.FestivalID
return RedirectToAction("Details", "Festival", new { id = Newevent.FestivalID });
//return RedirectToAction("Index", "Festival");
//return RedirectToAction("Index", "Events");
//String test = "test3";
}
ModelState.AddModelError("", "No Event Type Picked");
}
model.eType = db.EType.ToDictionary(p => p.ID, q => q.EType);
model.eType.Add(-1, "----- Add New Event Type -----");
model.eventsDate = DateTime.Now;
model.startTime = DateTime.Now;
model.endTime = DateTime.Now;
return View(model);
}
答案 1 :(得分:0)
以下是@Overmachine的答案
[HttpGet]
public ActionResult Create2(int festID)
{
EventsVM events = new EventsVM { festivalID = festID };
events.eType = db.EType.ToDictionary(p => p.ID, q => q.EType);
events.eType.Add(-1, "----- Add New Event Type -----");
events.eventsDate = DateTime.Now;
events.startTime = DateTime.Now;
events.endTime = DateTime.Now;
return View(events);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create2(EventsVM model, int festID)
{
if (ModelState.IsValid != true)
{
if (model.selectedEType != -1)
{
//db.save stuff from create.
Events Newevent = new Events();
Newevent.EndTime = model.endTime;
Newevent.StartTime = model.startTime;
Newevent.EventsDate = model.eventsDate = DateTime.Now;
Newevent.EventsName = model.EventsName;
Newevent.EType = db.EType.Where(p => p.ID == model.selectedEType).Single();
Newevent.Location = model.Location;
//Caught here!!!
Newevent.FestivalID = model.festivalID = festID;
db.Events.Add(Newevent);
db.SaveChanges();
//Change the model.festivalID to Newevent.FestivalID
return RedirectToAction("Details", "Festival", new { id = Newevent.FestivalID });
//return RedirectToAction("Index", "Festival");
//return RedirectToAction("Index", "Events");
//String test = "test3";
}
ModelState.AddModelError("", "No Event Type Picked");
}
model.eType = db.EType.ToDictionary(p => p.ID, q => q.EType);
model.eType.Add(-1, "----- Add New Event Type -----");
model.eventsDate = DateTime.Now;
model.startTime = DateTime.Now;
model.endTime = DateTime.Now;
return View(model);
}