c# - setting database value to a property value of an object -
i'm trying create new object in database take value of database entry based on choice of dropdownlist.
in following example want new "domena" object hold value of property "cena" belongs class tld, property "cena" has been created , it's in database decimal value. should take value of "cena" tld class when given tld has been selected dropdownlist.
here's domena class model :
public class domena { public int domenaid { get; set; } public int tldid { get; set; } // foreign id public int klientid { get; set; } // irrelevant code omitted public decimal cena { get; set; } // value should copied "cena" tld class public virtual tld tld { get; set; } public virtual klient klient { get; set; }
}
here's tld class :
public class tld { public int tldid { get; set; } public string typ { get; set; } public decimal cena { get; set; } public virtual icollection<domena> domeny { get; set; } }
i've been looking @ : http://blogs.msdn.com/b/adonet/archive/2011/01/30/using-dbcontext-in-ef-feature-ctp5-part-5-working-with-property-values.aspx couldn't find solution self. ive tried code in http create action:
public actionresult create(tld cena) { viewbag.tldid = new selectlist(db.tlds, "tldid", "typ"); viewbag.klientid = new selectlist(db.klienci, "klientid", "firma"); var model = new domena { cena = cena.cena }; return view(); }
but value still 0.00. have no idea how accomplish this.
any welcomed.
edit after bcr answer :
i'm sorry haven't put data needed. have modified httpget , httppost methods since ive added dropdownmenu it. here :
httpget :
public actionresult create() { viewbag.tldid = new selectlist(db.tlds, "tldid", "typ"); viewbag.klientid = new selectlist(db.klienci, "klientid", "firma"); viewbag.statusid = new selectlist(db.statusdomeny, "statusid", "status"); return view(); }
httppost :
[httppost] [validateantiforgerytoken] public actionresult create(domena domena) { if (modelstate.isvalid) { db.domeny.add(domena); db.savechanges(); return redirecttoaction("index"); } viewbag.tldid = new selectlist(db.tlds, "tldid", "typ", domena.tldid); viewbag.klientid = new selectlist(db.klienci, "klientid", "firma", domena.klientid); viewbag.statusid = new selectlist(db.statusdomeny, "statusid", "status", domena.statusid); return view(domena); }
my view looks :
<div class="editor-label"> @html.labelfor(model => model.tldid, "tld") </div> <div class="editor-field"> @html.dropdownlist("tldid", string.empty) @html.validationmessagefor(model => model.tldid) </div> <div class="editor-label"> @html.labelfor(model => model.klientid, "klient") </div> <div class="editor-field"> @html.dropdownlist("klientid", string.empty) @html.validationmessagefor(model => model.klientid) </div> <div class="editor-label"> @html.labelfor(model => model.statusid, "status") </div> <div class="editor-field"> @html.dropdownlist("statusid", string.empty) @html.validationmessagefor(model => model.statusid) </div>
as of now, can see i've deleted related "cena" couldn't work.
as understand in httpget method line :
model.tlddropdown = new selectlist(db.tlds, "tldid", "typ");
should
model.tldlist = new selectlist(db.tlds, "tldid", "typ");
based on you've put in createviewmodel :
public ienumerable<selectlistitem> tldlist {get; set;}
am right ?
however, when try this, im getting error msg saying :
cannot implicitly convert type 'system.web.mvc.selectlist' ienumearble. explicit conversion exist. missing cast?
maybe more explanation of i'm trying achive :
i have create page domena objects. there dropdownlist list of tlds db.tlds listed tld.typ. want httpget / httppost methods know if let's tldid = 1 (it's displayed tld.typ value dropdownlist) chosen should assign value of "cena" belongs tldid = 1 domena.cena property.
hope explains better.
thanks clarification, helps quite bit. view bound domena
entity model? that's not in view sample, i'm going assume so.
basically, once tld id user selected, you'll need tld entity context, , tld entity holds cena value want populate new domena object's property. i'm not sure whether/where you'd want put cena value on screen, have property of domenaviewmodel
, set in post action, after you've saved new domena
db.
assuming domena
classs entity, it's not great use entity view model, has been discussed many times here. general approach seems to have domenaviewmodel
class or domenadto
class use view model. map appropriate fields , view model , entity needed. automapper
popular option automate , organize process; use valueinjecter.
in particular example, doing manually should simple enough illustrate point.
another thing viewbag
used selectlists
viewbag
never referenced in view
. i'd away using viewbag
altogether here , stick viewmodel
approach. i'll keep simple, focusing on tld , domena classes, , needed create actions. similar approach used other drop-downs , entities.
note you'd want take care you're not adding domena
db 1 exists (has same foreign keys , values), unless that's want do. basing drop-down approach on scott allen's article.
public class domenaviewmodel { public list<tld> tlds {get; set;} public int selectedtldid { get; set; } public ienumerable<selectlistitem> tlditems { { return new selectlist(tlds, "tldid", "typ"); } } // irrelevant code omitted } [httpget] public actionresult create() { var model = new domenaviewmodel { tlds = db.tlds.tolist(); }; return view(model); } [httppost] [validateantiforgerytoken] public actionresult create(domenaviewmodel viewmodel) { if (modelstate.isvalid) { var tld = db.tlds.find(model.selectedtldid); // assuming you're using dbcontext if (tld != null) { // mapping best served in different method, we'll here var newdomena = new domena { tld = tld, cena = tld.cena } db.domeny.add(newdomena); db.savechanges(); } return redirecttoaction("index"); } viewmodel.tlds = db.tlds.tolist(); return view(viewmodel); }
the relevant part of view:
@model domenaviewmodel <div class="editor-field"> @html.dropdownlistfor(m => m.selectedtldid, model.tlditems) </div>
Comments
Post a Comment