asp.net mvc - Cannot store values of different tables in a database -
this sponsor model
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; } } this orphan model
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; } } this orphan controller
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); } } i have added sponsor button in index view of orphan
@html.actionlink("sponsor", "sponsor",new { id= item.id }) the view of sponsor 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> after clicking sponsor button moves sponsor action of orphan controller.since orphan id obtained value passed index view , userid obtained session.it shows during method of orphan in post action of sponsor doesnt store value in database.help me how can store in database?
avoid reading values viewbag in controller. viewbag collection of data can exchanged between controller , view. sponsor method should this:
[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); } moreover, not returning sponsor view. how manage model if not returning it. use this:
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 fill sponsormodel here return in view } return view(sponsormodel); }
Comments
Post a Comment