user1031034
user1031034

Reputation: 856

How to make dropdownlist

How make a dropdownlist? [Edited - almost working code]

View:

<div class="editor-label">
        Firma
    <div class="editor-field">
        @Html.DropDownListFor(x => x.ID_firma, Model.firmaList)
        @Html.ValidationMessageFor(model => model.nazwa)
    </div>

Model:

 public class produktModel
{
    [Required(ErrorMessage="Proszę podać nazwę.")]
    public string nazwa { get; set; }

    [Required(ErrorMessage = "Proszę podać ilść produktu.")]
    public decimal ilosc { get; set; }

    [Required(ErrorMessage = "Proszę podać jednostkę produktu (np. kg, l, szt.).")]
    public string jednostka { get; set; }

    [Required(ErrorMessage = "Proszę podać cenę produktu.")]
    public decimal cena { get; set; }

    public string ID_firma { get; set; }

    public IEnumerable<SelectListItem> firmaList { get; set; }
}

Controller:

 public ActionResult dodaj()
    {

        var firma = baza.Firmas;
        var model = new produktModel
        {
            firmaList = firma.AsEnumerable().Select(x => new SelectListItem
            {
                Value = x.ID_firma.ToString(),
                Text = x.nazwa
            })
        };

        return View(model);
    }

    [HttpPost]
    public ActionResult dodaj(produktModel model)
    {

        Produkt prod = new Produkt();

        prod.nazwa = model.nazwa;
        prod.ilosc = model.ilosc;
        prod.jednostka = model.jednostka;
        prod.cena = model.cena;
         prod.ID_firma = model.ID_firma;  


        baza.Produkts.InsertOnSubmit(prod);
        baza.SubmitChanges();

        return RedirectToAction("zarzadzaj_produktami", "Produkt");
    }

It almost work... I have only one problem (I hope)... Value is string, and I save his value to database... (I don't now how to write it...)

prod.ID_firma = model.ID_firma;

prod.ID_firma is int. model.ID_firma is this value which is string. So I have an error:

Error 1 Cannot implicitly convert type 'string' to 'int?'

Upvotes: 0

Views: 931

Answers (2)

Rafay
Rafay

Reputation: 31033

change your model a bit, i have assumed the column names change them according to your code

   public class produktModel
        {
            [Required]
            public string name { get; set; }
            public decimal price { get; set; }
            [Required]
            public int companyID {get; set;}
            public List<Company> compList {get; set;} 
        }

    public class Company{
        public int CompanyID {get;set;}
        public string CompanyName {get;set;}
       }

ActionResult should look like

public ActionResult add()
        {
            produktModel model = new produktModel();
            model.compList= (from b in base.Companies
                               select new Company{
                                 CompanyID = b.CompanyID,
                                 CompanyName = b.CompanyName
                                }).ToList();

            return View(model);
        }

in your (strongly typed) view

@model produktModel

....
<div class="editor-label">
            Company
 <div class="editor-field">
            @Html.DropDownListFor(model => model.companyID,
                                new  SelectListItem(model.compList,
                                           "CompanyID ",
                                           "CompanyName "))
            @Html.ValidationMessageFor(model => model.company_name)
        </div>
...

Upvotes: 1

gdoron
gdoron

Reputation: 150253

Your question isn't clear enough.
Any way you can use the telerik combox\dropdown list or the default mvc dropdown list. you can find a lot of examples on google for that.

With Telerik write something like this:

@(Html.Telerik().ComboBox()
            .Name("ComboBox")
            .BindTo(new SelectList("CompanyID", "CompanyName")))

see this Telerik demo for more information.

Upvotes: 0

Related Questions