Mvccheckboxlist, Radiobuttons and Text Inputs

Asked

Viewed 434 times

2

I have a project where I’m using Mvccheckboxlist. My question is this::

  • Could I put a mvccheckboxlist together with radio Buttons and inputs ? Because I am in a project at a school that I have to record some occurrences and I need that in these occurrences, when marking a check box, an input appears, but only in some. Explaining better, it would be the radio Buttons for the type of occurrence (Warning or Suspension) and the mvccheckboxlist for the causes (mess, swearing, etc.) and when choosing some causes appear a text input to complement. That is, relate the radio button to the check box. And after that I recover, edit, delete and add other occurrences. Remembering here that I already used the mvccheckboxlist, but I do not know how to record their values in the bank, and I already have the model of Student, which makes the register of students. In case of occurrence I would have to create another table ?

Anyway, I’m going to post the html code here, so you have an idea of how I wanted it to look:

<div class="panel-group" id="accordion">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#ocorrencias">
            Ocorrências do Aluno
        </a>
     </h4>
  </div>

  <div id="ocorrencias" class="panel-collapse collapse in">
    <div class="panel-body">
        <div class="radio-inline">
            <label class="radio-inline">
                <input type="radio" value="Advertencia" id="Advertencia" />
                Advertência
            </label>
            <label class="radio-inline">
                <input type="radio" value="Suspensao" id="Suspensao" />
                Suspensão por: <input type="text" class="col-xs-1" name="dias" id="dias"/> dias.
            </label>
        </div>

        <div class="checkbox">
            <label>
                <input type="checkbox" name="atividade" id="atividade" value="" checked>
                Estar deixando de fazer as atividades discentes ou não ter concluido a mesma
            </label>
        </div>

        <div class="checkbox">
            <label>
                <input type="checkbox" name="material" id="material" value="">
                Não trazer o material necessario para as aulas do dia
            </label>
        </div>

        <div class="checkbox">
            <label>
                <input type="checkbox" name="uniforme" id="uniforme" value="">
                Não estar devidamente uniformizado
            </label>
        </div>

        <div class="checkbox">
            <label>
                <input type="checkbox" name="conversa" id="conversa" value="">
                Estar conversando excessivamente em sala de aula
            </label>
        </div>

        <div class="checkbox">
            <label>
                <input type="checkbox" name="atrapalhando" id="atrapalhando" value="">
                Estar brincando e atrapalhando os colegarr em sala de aula
            </label>
        </div>

        <div class="checkbox">
            <label>
                <input type="checkbox" name="atividadesDocentes" id="atividadesDocentes" value="">
                Estar impedindo as atividades docentes
            </label>
        </div>

        <div class="checkbox">
            <label>
                <input type="checkbox" name="brincando" id="brincando" value="">
                Estar com brincadeira de mau gosto na escola <input type="text" name="motivo" id="motivo"/>
            </label>
        </div>

        <div class="checkbox">
            <label>
                <input type="checkbox" name="aula" id="aula" value="">
                Estar frequentemente "matando aula" ou chegando atrasado
            </label>
        </div>

        <div class="checkbox">
            <label>
                <input type="checkbox" name="fugindo" id="fugindo" value="">
                Ter saído da escola sem autorização, fugiu ás <input type="text" name="hora" id="hora"/>
            </label>
        </div>

        <div class="checkbox">
            <label>
                <input type="checkbox" name="foraDeSala" id="foraDeSala" value="">
                Estar fora da sala de aulas <input type="text" name="fora" id="fora"/>
            </label>
        </div>

        <div class="checkbox">
            <label>
                <input type="checkbox" name="agredido" id="agredido" value="">
                Ter agredido, faltado com o devido respeito aos colegas <input type="text" name="respeito" id="respeito" />
            </label>
        </div>

        <div class="checkbox">
            <label>
                <input type="checkbox" name="respeito" id="respeito" value="">
                Ter faltado com respeito ao professor
            </label>
        </div>

        <div class="checkbox">
            <label>
                <input type="checkbox" name="equipamentos" id="equipamentos" value="">
                Estar usando equipamentos eletro-eletrônicos em sala de aula <input type="text" name="equipamentos" id="equipamentos" />
            </label>
        </div>

        <div class="checkbox">
            <label>
                <input type="checkbox" name="outros" id="outros" value="">
                Outros <textarea name="outros" id="outros"></textarea>
            </label>
        </div>

        <div class="form-group">
            <input type="submit" value="Salvar" class="btn btn-success" />
        </div>

     </div>
  </div>

If you need more information, I put here, model or controller to make it clearer.

  • Do you only want to show the text? Each text is linked to a property in the model?

  • What happens is that I haven’t even created the occurrences model yet, because I don’t know how to relate this occurrences model to the student model. And wanted to record, edit, list and remove these occurrences without the student’s data being affected !

1 answer

2


I made some adjustments in input, radio and checkbox names that I didn’t have and were wrong or repeated

HTML

@model WebApplication2.Models.BaseModel
@{
    ViewBag.Title = "Listas";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Listas</h2>
@using (Html.BeginForm())
{
    <div class="panel-group" id="accordion">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a data-toggle="collapse" data-parent="#accordion" href="#ocorrencias">
                        Ocorrências do Aluno
                    </a>
                </h4>
            </div>
            <div>
                @Html.CheckBoxListFor(
        p => p.BaseSend.Send,
        p => p.BaseAll,
        p => p.Id,
        p => p.Name,
        b => b.BaseSelected
    )
            </div>

            <div id="ocorrencias" class="panel-collapse collapse in">
                <div class="panel-body">
                    <div class="radio-inline">
                        <label class="radio-inline">
                            <input type="radio" value="true" id="advertencia" name="advertencia" />
                            Advertência
                        </label>
                        <label class="radio-inline">
                            <input type="radio" value="true" id="suspensao" name="suspensao" />
                            Suspensão por: <input type="text" class="col-xs-1" name="dias" id="dias" /> dias.
                        </label>
                    </div>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="atividade" id="atividade" value="true">
                            Estar deixando de fazer as atividades discentes ou não ter concluido a mesma
                        </label>
                    </div>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="material" id="material" value="true">
                            Não trazer o material necessario para as aulas do dia
                        </label>
                    </div>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="uniforme" id="uniforme" value="true">
                            Não estar devidamente uniformizado
                        </label>
                    </div>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="conversa" id="conversa" value="true">
                            Estar conversando excessivamente em sala de aula
                        </label>
                    </div>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="atrapalhando" id="atrapalhando" value="true">
                            Estar brincando e atrapalhando os colegarr em sala de aula
                        </label>
                    </div>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="atividadesDocentes" id="atividadesDocentes" value="true">
                            Estar impedindo as atividades docentes
                        </label>
                    </div>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="brincando" id="brincando" value="true">
                            Estar com brincadeira de mau gosto na escola <input type="text" name="tmotivo" id="tmotivo" />
                        </label>
                    </div>

                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="aula" id="aula" value="true">
                            Estar frequentemente "matando aula" ou chegando atrasado
                        </label>
                    </div>

                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="fugindo" id="fugindo" value="true">
                            Ter saído da escola sem autorização, fugiu ás <input type="text" name="thora" id="thora" />
                        </label>
                    </div>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="foradesala" id="foradesala" value="true">
                            Estar fora da sala de aulas <input type="text" name="tfora" id="tfora" />
                        </label>
                    </div>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="agredido" id="agredido" value="true">
                            Ter agredido, faltado com o devido respeito aos colegas <input type="text" name="trespeito" id="trespeito" />
                        </label>
                    </div>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="respeito" id="respeito" value="true">
                            Ter faltado com respeito ao professor
                        </label>
                    </div>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="equipamentos" id="equipamentos" value="true">
                            Estar usando equipamentos eletro-eletrônicos em sala de aula <input type="text" name="tequipamentos" id="tequipamentos" />
                        </label>
                    </div>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="outros" id="outros" value="true">
                            Outros <textarea name="toutros" id="toutros"></textarea>
                        </label>
                    </div>
                    <div class="form-group">
                        <input type="submit" value="Salvar" class="btn btn-success" />
                    </div>
                </div>
            </div>
        </div>
    </div>
}

Actionresult (Methods)

public ActionResult Listas()
{
    BaseModel baseModel = new BaseModel();

    baseModel.BaseAll = new List<Base>(){
        new Base() { Id = 1, Name="Produto 1", IsSelected = true, Tags="Produtos"},
        new Base()  {Id = 2, Name="Produto 2", IsSelected = false, Tags="Produtos"}
    };

    baseModel.BaseSelected = new List<Base>(){
         new Base() { Id = 1, Name="Produto 1", IsSelected = true, Tags="Produtos"},
    };

    return View(baseModel);
}

1 - This is one of the ways to recover, but, I don’t really like working with Formcollection because of the mistakes and treatments we have to make

[HttpPost]
public ActionResult Listas(BaseSend BaseSend, FormCollection form)
{
    bool advertencia = false;
    bool suspensao = false;

    bool.TryParse(form.Get("advertencia"), out advertencia);
    bool.TryParse(form.Get("suspensao"), out suspensao);

    // e assim por diante quando for caixa de texto seria Int32 ou String

    int dias = 0;
    int.TryParse(form.Get("dias"), out dias);

    String trespeito = form.Get("trespeito");

    //são exemplos podem causar erros se não existir tá indice

    return RedirectToAction("Listas");
}

2 - Use this form, because, the data would already have a type, realize that the checkbox all have value="true" this is important for it to recover a boleano value and string so that it loads the text boxes, also note dias i put int, if the die placed in the box is number it will recover that number for you ...

[HttpPost]
public ActionResult Listas(BaseSend BaseSend, 
    bool? advertencia, 
    bool? suspensao,
    bool? atividade,
    bool? material,
    bool? uniforme,
    bool? conversa,
    bool? atrapalhando,
    bool? atividadesdocentes,
    bool? brincando, 
    string tmotivo,
    int? dias,
    bool? aula,
    bool? fugindo,
    bool? foradesala,
    string tfora,
    bool? agredido,
    string trespeito,
    bool? respeito,
    bool? equipamentos,
    string tequipamentos,
    bool? outros,
    string toutros)
{
    if (advertencia.HasValue)
    {

    }
    // e assim vai comparando todo os campos
    return RedirectToAction("Listas");
}
  • Extremely useful face, very good even ! Opened my mind ! Now, how would I relate these occurrences to the student, so that it is recorded in the bank with the student occurrence, beyond the data of course, attach to each other. And that way that you’re here, I could list, save, edit and remove if that’s the case ?

  • Yes if you have to pass the student id in a Hidden field

  • And will the event controller be of this model, or will I have to put everything in the same controller? Since I’m going to have to create a model just for the occurrences.

  • I like to share the responsibility, actually I use POO in its same concept, ie I do each in your controller, I use ajax to solve recording in other controllers, etc. There are those who use a controller for a certain problem / solution. but, this goes from programmer to programmer or solution to solution, although you can use a repository class to improve the solution. It has several forms but, it will depend on your understanding. Doubts tamos ai!

  • In fact, I also much prefer each one in its place. In fact, it’s a mess of code. I want it to be organized, so I prefer to do each one in your controller ! So, how would I use ajax to solve this recording ? Because I want to put the occurrences views in the student’s detail view. To get one thing. How could I proceed ? @Fccdias when you can let’s chat, it can be ?

  • if you have an example for me to base on, it would be great !

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.