0

I'm working with VS2010 with ASP.net MVC4

I need to create a component with checkbox in this case the component must be integrated with three checkbox, in the image that should be attached as the third column "Esado de la Pieza". (I know I solve it by inserting a dropdownlist, but the customer wants to checkbox). How I can create this component? or solve this problem without using dropdownlist? if data is loaded from a query to the database.

enter image description here

raranibar
  • 1,247
  • 4
  • 23
  • 44
  • I would recommend using a ViewModel, http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc Your viewModel can have three properties "Bueno", "Regular", "Deficiente", and when you are going to Map your viewModel with your business model you make a logic to set the value of "Estado de la pieza" equals to the viewModel's property selected. If you want more info about it I can write a proper answer. – Guillermo Oramas R. Oct 30 '13 at 18:30
  • Hi Guillelon, yes I need more info about it. – raranibar Nov 01 '13 at 13:29

1 Answers1

0

The first thing I'm going to say is that I know that this way is not "answering" your question about creating a custom checkbox, but rather is giving you what I consider a better way to handle with this kind of situations. When we are handling with CRUD views, sometimes our business objects don't have the necessary information that the views need, or in cases they have much more than we need. So I recommend using ViewModels.

The viewModel would be something like this (I'm just putting the first property, Mascara, and assume that your object is called Form):

public class FormViewModel()
{
    public int MascaraCant { get; set; }
    public Boolean MascaraBueno { get; set; }
    public Boolean MascaraRegular { get; set; }
    public Boolean MascaraDeficiente { get; set; }
    public string MascaraObservaciones { get; set; }
}

That's the object your controller is going to send from the get method to the view, and then receive in the post method from the view. Your controller would be something like this.

public class FormController: Controller
{
    public ActionResult Create()
    {
        return View(new FormViewModel());
    }

    [HttpPost]
    public ActionResult Create(FormViewModel model)
    {
        var form = new Form();
        //Properties that no need any logic to be mapped
        form.MascaraCant= model.MascaraCant;
        form.MascaraObservaciones = model.MascaraObservaciones;

        //This is where I put the logic to be mapped
        if(model.MascaraBueno)
            form.EstadoDeLaPieza = "Bueno";
        else if(model.MascaraRegular)
            form.EstadoDeLaPieza = "Regular";
        else if(model.MascaraDeficiente)
            form.EstadoDeLaPieza = "Deficiente";
        Save(form);
        return View(new FormViewModel());
    }
}

So as you can see, all I do is to use a different object to communicate my controllers with the view, so I can play with the different tools needed in each views. Could be available data from a list, like Cities, countries, etc.

For the properties that no need a logic to be mapped like MascaraCant I would recommend you a nugget package called AutoMapper

Hope it helped.

Guillermo Oramas R.
  • 1,303
  • 2
  • 14
  • 31