在视图MVC5之间传递值
本文关键字:之间 视图 MVC5 | 更新日期: 2023-09-27 18:23:43
我正在创建一个事件管理系统,我想创建一个活动,然后为该活动创建多个票证。我使用的是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; }
}
我已经为事件使用了脚手架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")
}
我已经在我的剃刀视图中使用了表单,事件正在正确地保存到数据库中,EventID正在Ticket中设置并传递到SaveTicket视图。但是,当我尝试保存票证时会出现问题,票证不会保存,页面只是刷新。我尝试了很多教程,但都没有给我提供解决方案,我已经被困了一周的大部分时间。
您的问题可能是没有告诉<form>
将数据发布到哪里。
当您只使用@using (Html.BeginForm())
时,它假设您想将数据发布回视图来源的同一url,或者地址栏中的内容。
由于返回视图的操作是SaveTickets
,因此数据将发布到http://host/Events1/SaveTickets
,但您的[HttpPost]
操作是AddToTickets
。
你也可以将你的[HttpPost]
操作重命名为SaveTickets
,这将是最简单的,或者告诉你的表单你想发布的url/操作是什么。
@using (Html.BeginForm("AddToTickets","Events1"))
这可能会解决问题,但你想接受斯蒂芬的建议,为Tickets创建一个单独的控制器。。