我有一个用户输入他/她的详细信息的表单。
@using (Html.BeginForm("Create","Coupons"))
{
<div class="row">
<div class="col-sm-8">
<div class="page-header">Generer Coupon</div>
</div>
</div>
<div class="row">
<div class="col-sm-4">
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(m => m.Student)
@Html.DropDownListFor(m => m.Student, new SelectList(Model.Students, "Id", "Name"), "", new { @class = "form-control input-lg" })
</div>
</div>
</div>
<div class="row">
<div class="col-sm-4">
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(m => m.Price)
@Html.TextBoxFor(m => m.Price, new { @class = "form-control input-lg" })
</div>
</div>
</div>
<div class="row">
<div class="col-sm-4">
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(m => m.NumberOfCoupons)
@Html.TextBoxFor(m => m.NumberOfCoupons, new {@class = "form-control input-lg"})
</div>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<button class="btn btn-success btn-lg">Generer</button>
</div>
</div>
}
我将视图重定向到CouponsController上的Create Method
[HttpPost]
public ActionResult Create(CouponViewModel viewModel)
{
if (!ModelState.IsValid)
{
var model = new CouponViewModel
{
Students = _context.Students.ToList()
};
return View("Index", model);
}
for (int i = 1; i <= viewModel.NumberOfCoupons; i++)
{
var coupon = new Coupon
{
CouponNumber = viewModel.CouponNumber,
ValidationCode = viewModel.ValidationCode(6),
Price = viewModel.Price,
StudentId = viewModel.Student,
DateTime = Convert.ToDateTime(DateTime.Now.ToString("yy-MMM-dd ddd"))
};
_context.Coupons.Add(coupon);
_context.SaveChanges();
}
var coupons = _context.Coupons
.Where(c => c.StudentId == viewModel.Student)
.Include(s => s.Student)
.ToList();
TempData["viewModel"] = coupons;
return RedirectToAction("GetCoupons");
}
这是我收到要在视图上显示的tempdata的视图
[HttpPost]
public ActionResult GetCoupons()
{
Coupon coupon = TempData["viewModel"] as Coupon;
return View("Print", (IEnumerable<Coupon>)coupon);
}
我被困了3天。我不知道我错在哪里
答案 0 :(得分:1)
您的示例存在一些问题。但如果你采取不同的做法,你可以消除它们。
map.keys.map(_.stripPrefix("dealership_id_").toInt).max // 39
首先,您的重定向操作需要标记为[HttpPost]
public ActionResult Create(CouponViewModel viewModel)
{
return RedirectToAction("GetCoupons", new { studentId = viewModel.Student });
}
。如果在查询字符串上传递id,则还可以避免[HttpGet]
,然后在重定向的操作中执行查找。
TempData
[HttpGet]
public ActionResult GetCoupons(int studentId)
{
var coupons = _context.Coupons
.Where(c => c.StudentId == studentId)
.Include(s => s.Student)
.ToList();
return View("Print", coupons);
}
并没有错,但它不会在刷新时继续存在。有时您会传递敏感数据,这使得它很有用。您投放到单个TempData
的投射并不是您想要的 - 转而投射到集合。
Coupon