这是我的赞助商模式
public enum PaymentType
{
Cash,
CreditCard,
Cheque
}
public class SponsorDetail
{
public int Id { get; set; }
public Orphan Orphan { get; set; }
public int OrphanId { get; set; }
public User User { get; set; }
public int UserId { get; set; }
public PaymentType PaymentType{ get; set; }
public int PaymentNo { get; set; }
public DateTime DateOfReceipt { get; set; }
}
这是孤儿模型
public class Orphan
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public bool Disable { get; set; }
public DateTime JoinedDate { get; set; }
public DateTime? LeaveDate { get; set; }
public Supervisor Supervisor { get; set; }
public string SupervisorName { get; set; }
public string Picture { get; set; }
}
这是孤儿控制器
public class OrphanController : DatabaseController
{
public ActionResult Index()
{
return View(db.Orphans.ToList());
}
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Orphan orphan = db.Orphans.Find(id);
if (orphan == null)
{
return HttpNotFound();
}
return View(orphan);
}
public ActionResult Sponsor(int? id )
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
if (Session["UserId"] != null)
{
ViewBag.OrphanId = id;
ViewBag.UserId = Session["UserId"];
}
return View();
}
[HttpPost]
public ActionResult Sponsor(SponsorDetail sponsor,int? id)
{
if (ModelState.IsValid)
{
ViewBag.OrphanId = id;
ViewBag.UserId = Session["UserId"];
db.SponsorDetails.Add(new SponsorDetail()
{
OrphanId =(int) ViewBag.OrphanId,
UserId=(int)ViewBag.UserId,
PaymentType = sponsor.PaymentType,
PaymentNo =sponsor.PaymentNo,
DateOfReceipt = DateTime.Now
});
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.OrphanId = new SelectList(db.Orphans, "Id", "FirstName",sponsor.OrphanId);
return View(sponsor);
}
}
我在孤儿的索引视图中添加了一个赞助商按钮
@Html.ActionLink("Sponsor", "Sponsor",new { id= item.Id })
赞助商对userid和orphanid的看法
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.OrphanId, "OrphanId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Editor("OrphanId", new { htmlAttributes = new { @class = "form-control", disabled = "disabled" } })
@Html.ValidationMessageFor(model => model.OrphanId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserId, "UserId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Editor("UserId", new { htmlAttributes = new { @class = "form-control", disabled = "disabled"} })
@Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })
</div>
</div>
点击赞助商按钮后,它将移动到孤儿控制器的赞助商行动。因为从索引视图传递的值获得了孤儿ID,并且从会话中获取了用户ID。它在孤儿的Get方法中显示但在后期操作中显示赞助商它并没有将数值存储在数据库中。帮助我如何将其存储在数据库中?
答案 0 :(得分:0)
避免从控制器中的ViewBag读取值。 ViewBag只是可以在控制器和视图之间交换的数据集合。你的赞助商方法应该是这样的:
[HttpPost]
public ActionResult Sponsor(SponsorDetail sponsor,int? id)
{
if (ModelState.IsValid)
{
ViewBag.OrphanId = id;
ViewBag.UserId = Session["UserId"];
db.SponsorDetails.Add(new SponsorDetail()
{
OrphanId =(int)id,
UserId= Convert.ToInt32(Session["UserId"]),
PaymentType = sponsor.PaymentType,
PaymentNo =sponsor.PaymentNo,
DateOfReceipt = DateTime.Now
});
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.OrphanId = new SelectList(db.Orphans, "Id", "FirstName",sponsor.OrphanId);
return View(sponsor);
}
此外,您不会将任何赞助商退回到视图中。如果您没有退回,您将如何管理您的模型。 我会这样用:
public ActionResult Sponsor(int? id )
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
if (Session["UserId"] != null)
{
ViewBag.OrphanId = id;
ViewBag.UserId = Session["UserId"];
//you need to fill the sponsorModel here an return it back in the view
}
return View(sponsorModel);
}
答案 1 :(得分:0)
我做了这些改变,感谢大家帮助我。
public ActionResult Sponsor(int? id )
{
if (Session["UserId"] != null)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
else
{
ViewBag.OrphanId = id;
ViewBag.UserId = Session["UserId"];
}
}
else
{
return RedirectToAction("Login", "Login");
}
return View();
}
[HttpPost]
public ActionResult Sponsor(SponsorDetail sponsor,int? id)
{
if (ModelState.IsValid)
{
db.SponsorDetails.Add(new SponsorDetail()
{
OrphanId = sponsor.OrphanId,
UserId=sponsor.UserId,
PaymentType = sponsor.PaymentType,
PaymentNo =sponsor.PaymentNo,
DateOfReceipt = DateTime.Now
});
db.SaveChanges();
ViewBag.Message = "Successful";
return RedirectToAction("Index");
}
ViewBag.OrphanId = new SelectList(db.Orphans, "Id", "FirstName",sponsor.OrphanId);
return View(sponsor);
}