How to put a dropdownist in the edit method for mvc 5?

0

I have a problem adding a DropDownList , RadioButton and CheckBox in the edit method, I do not know how to put it in the view, or what I have to modify in the class or the controller

The class:

 public class Bills
    {
        public int ID { get; set; }
        public string Concepto { get; set; }
        public int Monto { get; set; }
        public Banco Banco { get; set; }
        public FormaPago Pago { get; set; }
        public string Factura { get; set; }
        [Display(Name = "Fecha")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime Fecha_de_Pago { get; set; }
        public bool Aprobacion { get; set; }
        //public ICollection<Enrollment> Enrollments { get; set; }
    }


    public enum Banco
    {

        Hsbc=1, 
        American_Express=2,
        BBVA_Bancomer=3,
        Banamex=4


    }
     public enum FormaPago
    {
        Efectivo=1,
        Pago_en_linea=2,
        Cheque=3,
        Tarjeta=4
    }

    public class ConceptoPagoDBContext : DbContext
    {
        public DbSet<Bills> Pago { get; set; }
    }

    //el controlador
    public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Bills bills = db.Pago.Find(id);
            if (bills == null)
            {
                return HttpNotFound();
            }
            return View(bills);
        }

        // POST: /Concepto/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="ID,Concepto,Monto,Banco,Pago,Factura,Fecha_de_Pago,Aprobacion")] Bills bills)
        {
            if (ModelState.IsValid)
            {
                db.Entry(bills).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(bills);
        }
    //la vista de edit 
    @model Pagos.Models.Bills

    @{
       ViewBag.Title = "Edit";
     }

     <h2>Edit</h2>


     @using (Html.BeginForm())
    {
       @Html.AntiForgeryToken()

        <div class="form-horizontal">
         <h4>Pagos</h4>
         <hr />
         @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.ID)

        <div class="form-group">
            @Html.LabelFor(model => model.Concepto, new { @class = "control-           label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Concepto)
                @Html.ValidationMessageFor(model => model.Concepto)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Monto, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Monto)
                @Html.ValidationMessageFor(model => model.Monto)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Banco, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
            @Html.DropDownListFor(model => model.Banco,
                    new SelectList(Enum.GetNames(typeof(Pagos.Models.Banco))),
                    "Seleciona Banco")
            </div>    
        </div>    


        <div class="form-group">
            @Html.LabelFor(model => model.Pago, new { @class = "control-label col-md-2" })
            @Html.DropDownListFor(model => model.Pago,
                    new SelectList(Enum.GetNames(typeof(Pagos.Models.FormaPago))),
                    "Seleciona Forma de Pago")
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Factura, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <p>¿Desea factura?</p>
                SI:   @Html.RadioButton("Factura", "Si")
                NO: @Html.RadioButton("Factura", "No")
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Fecha_de_Pago, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Fecha_de_Pago)
                @Html.ValidationMessageFor(model => model.Fecha_de_Pago)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Aprobacion, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <p>¿Son correctos los datos?</p>
                @Html.CheckBox("Aprobacion", false)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
  }

    <div>
       @Html.ActionLink("Back to List", "Index")
    </div>
    
asked by Fernanda 24.05.2016 в 19:36
source

2 answers

1

For the DropDownList you can use the EnumDropDownListFor method:

@Html.EnumDropDownListFor(model => model.Banco, "Selecciona Banco")

@Html.EnumDropDownListFor(model => model.Pago, "Seleccione Forma de Pago")

If you want you can customize the text to be displayed for each enumeration option using DataAnnotations:

public enum Banco
{
    Hsbc = 1,
    [Display(Name = "American Express")]
    American_Express = 2,
    [Display(Name = "BBVA")]
    BBVA_Bancomer = 3,
    Banamex = 4
}
public enum FormaPago
{
    [Display(Name = "En efectivo")]
    Efectivo = 1,
    [Display(Name = "Pago online")]
    Pago_en_linea = 2,
    Cheque = 3,
    Tarjeta = 4
}

For the checkbox you can use the CheckBoxFor method:

@Html.CheckBoxFor(model => model.Aprobacion)

For radiobuttons you should use the RadioButtonFor method:

SI: @Html.RadioButtonFor(model => model.Factura, "Sí")
NO: @Html.RadioButtonFor(model => model.Factura, "No")

Note that the value of the Invoice property must match one of the values indicated in the options (in my example "Yes" or "No").

    
answered by 24.05.2016 в 20:25
0

To define a checkbox you could use

@Html.CheckBoxFor(m=> m.Aprobacion)

for the dropdownlist you would use

@Html.DropDownListFor(m=> m.Banco, ViewBag.ListaBancos)

but you need to send the list in the ViewBag the list of banks that you get from the enum

[TIP] Obtaining the Names of an Enumerated

As you will notice in the link, it gives you a clue, you could define a class

public class EnumList
{
    public int Key {get;set;}
    public int Desc {get;set;}
}   

and then use

var query = (from int n in Enum.GetValues(typeof(Banco))
             select new EnumList() {
                     Key = n,
                     Desc = Enum.GetName(typeof(Banco),n)
                 });

the result of the linq you will have the list mapped to a class that you can assign the ViewBag to list in the combo

    
answered by 24.05.2016 в 20:20