gpt4 book ai didi

javascript - 在 View 之间传递值 MVC5

转载 作者:行者123 更新时间:2023-11-28 06:41:01 24 4
gpt4 key购买 nike

我正在创建一个事件管理系统,我想创建一个事件,然后为此事件创建多个门票。我正在使用 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 View ,然后我想将我创建的事件的 EventID 传递到 AddTicket View 并创建特定于该事件的新票证。这是我的 Controller 类;

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")
}

我在我的 razor View 中使用了表单,并且事件正确保存到数据库中,并且 EventID 正在票证中设置并传递到 SaveTicket View 。但是,当我尝试保存票证时,问题出现了,票证没有保存,页面只是刷新。我已经尝试了很多教程,但还没有一个为我提供解决方案,并且我已经被困了一周的大部分时间。

最佳答案

您的问题可能是您没有告诉您的<form>在哪里发布数据。

当你只使用 @using (Html.BeginForm()) 时它假设您要将数据发送回 View 来源的同一 URL,或者地址栏中的内容。

由于返回 View 的操作是 SaveTickets那么数据将发布到http://host/Events1/SaveTickets但是你的[HttpPost]行动是 AddToTickets .

您可以重命名 [HttpPost]行动至SaveTickets同样,这将是最简单的,或者告诉您的表单您想要发布到的网址/操作是什么。

@using (Html.BeginForm("AddToTickets","Events1"))

这可能会解决问题,但您想采纳 Stephen 的建议并为 Tickets 创建一个单独的 Controller ..

关于javascript - 在 View 之间传递值 MVC5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33815145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com