I'm trying to create a new object in the database which will take a value of a database entry based on the choice of a DropDownList.
In the following example I want a new "Domena" object to hold the value of property "Cena" which belongs to Class TLD, the property "Cena" has been created and it's in the database as decimal value. It should take the value of a "Cena" from TLD Class when given TLD has been selected from the DropDownList.
Here's the 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 which should be copied from "Cena" from TLD CLASS
public virtual TLD TLD { get; set; }
public virtual Klient Klient { get; set; }
}
Here's the 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 at : http://blogs.msdn.com/b/adonet/archive/2011/01/30/using-dbcontext-in-ef-feature-ctp5-part-5-working-with-property-values.aspx but I couldn't find a solution for my self. What ive tried already is this code in my HTTP GET 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 the Value is still 0.00. I have no idea how to accomplish this.
Any help would be very welcomed.
Edit after bcr answer :
I'm sorry I haven't put all the data needed. I have actually modified the httpget and httpPost methods since ive added another dropdownmenu to it. Here they are :
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 like this :
<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, as you can see I've deleted anything related to "Cena" as I couldn't get it to work.
As I understand in the httpGet method the line :
model.TldDropDown = new SelectList(db.TLDs, "TLDID", "Typ");
should be
model.TldList = new SelectList(db.TLDs, "TLDID", "Typ");
based on what you've put in the CreateViewModel :
public IEnumerable<SelectListItem> TldList {get; set;}
Am I right ?
However, when I try this, im getting an error msg saying :
Cannot implicitly convert type 'System.Web.Mvc.SelectList' to IEnumearble. An explicit conversion exist. Are you missing a cast?
Maybe more explanation of what I'm trying to achive :
I have a Create page for Domena objects. There is a DropDownList for list of TLDs from db.TLDs listed by TLD.Typ. I want my httpGet / httpPost methods to know that if let's say TLDID = 1 (it's displayed TLD.Typ value from the DropDownList) was chosen then it should assign the value of "Cena" which belongs to TLDID = 1 to Domena.Cena property.
Hope that explains it better.