這是我的贊助商模式不能存儲在不同表的值在數據庫中
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 })
贊助商的用於用戶標識的視圖和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>
點擊贊助按鈕之後其移動到贊助孤兒controller.Since的動作的孤兒ID是從索引視圖和用戶ID傳遞的值而獲得是從會話中獲得的。它在孤兒的Get方法中顯示,但在發起者的後期操作中,它不存儲數據庫中的值。幫助我如何將它存儲在數據庫中?
你會得到什麼異常?它發回服務器? –
已禁用的控件不回發一個值。 –
而且在任何情況下,您都無法從POST方法中的'ViewBag'中讀取值 - 這僅用於將值從控制器傳遞到視圖。 –