使用ViewModel添加模型的多个实例
本文关键字:实例 模型 ViewModel 添加 使用 | 更新日期: 2023-09-27 18:26:35
我有模型Event和Ticket
public class Event
{
public int EventID { get; set; }
[Required]
public String Name { get; set; }
[Required]
public String Location { get; set; }
[Required]
[DataType(DataType.Date)]
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; }
}
我创建了两个视图模型,允许我为每个活动添加多个门票
public class TicketVm
{
[Required]
public string Description { get; set; }
[Required]
public float Price { get; set; }
}
public class CreateTicketVm
{
public int EventId { get; set; }
public List<TicketVm> Tickets { set; get; }
}
然后我有一个视图,它允许用户创建一个新的事件。然后,当他们提交此事件时,他们被引导到CreateTicket视图,该视图应该使用与创建的事件相同的EventID向此事件添加新的票证。
这是我的创建票证视图
@model Site.Models.CreateTicketVm
<head>
<title>Create Event Tickets</title>
<link href="Content/TicketStyles.css" rel="stylesheet" type="text/css">
</head>
<h3> Tickets</h3>
<div id="tickets"></div>
@Html.HiddenFor(s => s.EventId)
<input type="button" id="add" value="Add Ticket" />
<input type="submit" id="btnSave" />
@section scripts{
<script>
$(function () {
//Lets create the first row
addNewRow();
$("#add").click(function (e) {
e.preventDefault();
addNewRow();
});
$("#btnSave").click(function (e) {
e.preventDefault();
var createTicket = { EventId: $("#EventId").val(), Tickets: [] };
var rows = $("#tickets").find(".item");
$.each(rows, function (i, row) {
var _row = $(this);
var inputs = _row.find("input");
var ticket = {};
$.each(inputs, function (index, item) {
var _this = $(this);
ticket[_this.data("for")] = _this.val();
});
createTicket.Tickets.push(ticket);
});
var targetUrl = "@Url.Action("CreateTicket","Tickets1")";
$.ajax({
url: targetUrl,
contentType: "application/json",
data: JSON.stringify(createTicket),
type: "POST"
}).done(function (res) {
if (res.Status === "Success") {
window.location.href = "@Url.Action("Success")";
}
});
});
});
function addNewRow() {
$.get("@Url.Action("AddNewTicketRow","Tickets1")", function (res) {
$("#tickets").append(res);
});
}
</script>
}
这是我的AddNewTicket部分视图
@model Site.Models.TicketVm
<div class="item">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div>
@Html.TextBoxFor(model => model.Description, new { @data_for = "Description" })
</div>
@Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
<div>
@Html.TextBoxFor(s => s.Price, new { @data_for = "Price" })
</div>
</div>
这是我的Tickets1Controller类
public class Tickets1Controller : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult CreateTicket(int id)
{
var vm = new CreateTicketVm { EventId = id };
return View(vm);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateTicket(CreateTicketVm model)
{
if (model != null)
{
foreach (var ticketVm in model.Tickets)
{
db.Tickets.Add(new Ticket{ EventID = model.EventId,
Description=ticketVm.Description, Price =ticketVm.Price});
db.SaveChanges();
}
return RedirectToAction("Index");
}
return RedirectToAction("Index");
}
public ActionResult AddNewTicketRow()
{
var vm = new TicketVm();
return PartialView("~/Views/Tickets1/Partials/AddNewTicketRow.cshtml", vm);
}
我的Tickets没有保存,我不明白为什么,因为EventID被传递到CreateTicket视图,但当单击Submit按钮时没有发生任何事情,并且Tickets也没有保存到我的数据库表中。任何关于如何解决这个问题的建议都将不胜感激。
有一次我遇到了同样的问题。我所做的就是为页面定义一个jquery变量,并在每次单击"AddNewTicketRow"时增加它。然后将其作为参数发送到"AddNewTicketRow"控制器,并将其用作INDEX来创建AddNewTiket部分视图,如下所示:
@Html.TextBoxFor(M => M.Description, new { Name = "Tickets[" + @Model.Index + "].Description", id = "Tickets[" + @Model.Index + "].Description" })
@Html.TextBoxFor(M => M.Price, new { Name = "Tickets[" + @Model.Index + "].Price", id = "Tickets[" + @Model.Index + "].Price" })