我正在创建一个事件管理系统,我想创建一个事件,然后为此事件创建多个票证。我正在使用c#和ASP.NET MVC。我创建了这些模型类;
public class Event
{
public int EventID { get; set; }
[Required]
public String Name { get; set; }
[Required]
public String Location { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public String Description { get; set; }
[Required]
public int TicketsAvailable { get; set; }
//navigation property
public virtual ICollection<Order> Order { get; set; }
//navigation property
public virtual ICollection<Ticket> Ticket { get; set;}
}
public class Ticket
{
public int TicketID { get; set; }
[Required]
[ForeignKey("Event")]
//foreign key
public int EventID { get; set; }
[Required]
public string Description { get; set; }
[Required]
public float Price { get; set; }
//navigation property
public virtual Event Event { get; set; }
//navigation property
public ICollection<OrderDetails> OrderDetails { get; set; }
}
我已经为事件使用了Scaffolded CRUD视图,然后我想将我创建的事件的EventID传递给AddTicket视图并创建特定于事件的新票证。 这是我的控制器类;
public class Events1Controller : Controller
{
private IEventRepository _eventRepository;
private ITicketRepository _ticketRepository;
public Events1Controller()
{
this._eventRepository = new EventRepository(new ApplicationDbContext());
this._ticketRepository = new TicketRepository(new ApplicationDbContext());
}
// GET: Events
[AllowAnonymous]
public ActionResult Index()
{
return View(_eventRepository.GetEvents());
}
// GET: Events/Create
[AllowAnonymous]
public ActionResult Create()
{
return View();
}
// POST: Events/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public ActionResult Create([Bind(Include = "EventID,Name,Location,Date,Description,TicketsAvailable")] Event @event)
{
if (ModelState.IsValid)
{
Session["Event1"] = @event;
_eventRepository.InsertEvent(@event);
return RedirectToAction("SaveTickets");
}
return View();
}
[AllowAnonymous]
public ActionResult SaveTickets()
{
Event @e1 = Session["Event1"] as Event;
Ticket @ticket1 = new Ticket
{
EventID = @e1.EventID
};
return View(@ticket1);
}
// POST: Events/AddToTickets
[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public ActionResult AddToTickets([Bind(Include = "TicketID, EventID, Description, Price")] Ticket @ticket)
{
if (ModelState.IsValid)
{
_ticketRepository.InsertTicket(@ticket);
return RedirectToAction("Index");
}
return View();
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Event</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.TicketID)
<div class="form-group">
@Html.LabelFor(model => model.EventID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model => model.EventID, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Price,"", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="AddToTickets" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我在剃刀视图中使用了表单,事件正确保存到数据库,并且在Ticket中设置了EventID并传递给SaveTicket视图。但是,当我尝试保存票证时,问题就出现了,票证没有保存,页面只是刷新。我已经尝试了很多教程,但没有一个能为我提供解决方案,而且我已经陷入了一周的最佳状态。
答案 0 :(得分:0)
您的问题可能是您没有告诉<form>
在哪里发布数据。
当您使用@using (Html.BeginForm())
时,它会假定您要将数据发回到视图来自的相同网址或地址栏中的内容。
由于返回视图的操作为SaveTickets
,因此数据将发布到http://host/Events1/SaveTickets
,但您的[HttpPost]
操作为AddToTickets
。
您也可以将[HttpPost]
操作重命名为SaveTickets
,这是最简单的,或者告诉您的表单要发布的网址/操作是什么。
@using (Html.BeginForm("AddToTickets","Events1"))
这可能会解决问题,但你想接受斯蒂芬的建议并为门票创建一个单独的控制器..