Adding text box and saving to the bank. (with C #, entityframework 6.1.3)

3

I'm doing a project in Visual Studio 2013, with EntityFramework version 6.1.3, MVC 4.5.0.0 and using ViewModel .

I'm programming a page called payment condition. The creation page will have a text field nome , 2 radiobuttons : "the view" and "term". If the user selects the view nothing will happen.

Now, if the radiobutton "term" is selected, a button will appear to add textbox s of the term days to be filled. I need to do this schedule to add the textbox s as click the add days button and then do a function to write to the bank.

I think the function of the add days button can do in Jquery, and then the create button that will add a function in the Controller .

I know jquery, do add textboxes, now I'm a little lost in the C # part, writing to the bank made in Controller .

Thecodebelowisfrommyviewmodel:

[Key][DisplayName("Id")]
    public int CondicaoPagamentoId { get; set; }

    [Required(ErrorMessageResourceName = "PropertyValueRequired", ErrorMessageResourceType = typeof(DataAnnotationsResources))]
    [MinLength(DataConfig.DATABASE_MIN_LENGTH_DEFAULT, ErrorMessageResourceName = "MinLengthAttribute_ValidationError", ErrorMessageResourceType = typeof(DataAnnotationsResources))]
    [MaxLength(DataConfig.DATABASE_MAX_LENGTH_DEFAULT, ErrorMessageResourceName = "MaxLengthAttribute_ValidationError", ErrorMessageResourceType = typeof(DataAnnotationsResources))]
    [DisplayName("Nome")]
    public string Nome { get; set; }

    [Required(ErrorMessageResourceName = "PropertyValueRequired", ErrorMessageResourceType = typeof(DataAnnotationsResources))]
    [MinLength(DataConfig.DATABASE_MIN_LENGTH_DEFAULT, ErrorMessageResourceName = "MinLengthAttribute_ValidationError", ErrorMessageResourceType = typeof(DataAnnotationsResources))]
    [MaxLength(DataConfig.DATABASE_MAX_LENGTH_DEFAULT, ErrorMessageResourceName = "MaxLengthAttribute_ValidationError", ErrorMessageResourceType = typeof(DataAnnotationsResources))]
    [DisplayName("Tipo")]
    public bool Tipo { get; set; }

    [Required(ErrorMessageResourceName = "PropertyValueRequired", ErrorMessageResourceType = typeof(DataAnnotationsResources))]
    [DisplayName("Dias")]
    public int CondicaoPagamentoDiasId { get; set; }
    public virtual CondicaoPagamentoDiasViewModel CondicaoPagamentoDias { get; set; }
    public virtual IEnumerable<CondicaoPagamentoDiasViewModel> CondicaoPagamentoDiass { get; set; }

    [DisplayName("Cadastrado por")]
    public int UsuarioCadastroId { get; set; }
    public virtual UsuarioViewModel UsuarioCadastro { get; set; }

    [DisplayName("Data de Cadastro")]
    public DateTime DataCadastro { get; set; }

the addition is made by:

this.add(objeto)
    
asked by anonymous 21.09.2015 / 16:04

2 answers

3

Well, it's all okay for you to use BeginCollectionItem . Just now implement. My suggestion for benefits starts in the View code below:

<div class="condicoes-pagamento" id="condicoes-pagamento">
    @if (Model != null)
    {
        foreach (var condicoesPgto in Model.CondicaoPagamentoDiass)
        {
            @Html.Partial("_CondicoesPagamentoDia", condicoesPgto);
        }
    }
</div>

Partial _CondicoesPagamentoDia would look like this:

@model MeuProjeto.ViewModels.CondicaoPagamentoDiasViewModel

@using (Html.BeginCollectionItem("CondicaoPagamentoDiass"))
{
    <div class="form-group">
        @Html.HiddenFor(model => model.CondicaoPagamentoDiaId)
        @Html.HiddenFor(model => model.CondicaoPagamentoId)

        <label class="col-md-1 control-label">Dia:</label>
        <div class="col-md-3">
            @Html.TextBoxFor(model => model.Dia, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Dia, "", new { @class = "text-danger" })
        </div>
        <div class="col-md-2">
            <a class="btn red" onclick="$(this).parent().parent().remove();">Excluir</a>
        </div>
    </div>
}

Note that the code already assumes a row exclusion button in JavaScript. There is also an addition button that can be implemented as follows:

        <script type="text/javascript">
            $("#adicionar-dia").click(function () {
                $.get('/SeuController/NovaLinhaDia', function (template) {
                    $("#condicoes-pagamento").append(template);
                });
            });
        </script>

Notice that this code makes a call to a Controller method. It does not have to be this way. I use this way to perform server-side validations. The code looks like this:

    public ActionResult NovaLinhaDia()
    {
        return PartialView("_CondicoesPagamentoDia", new CondicaoPagamentoDiasViewModel());
    }

By doing the POST of the form, you will see that CondicaoPagamentoDiass will receive all the lines filled in on the form there in Action of Controller .

    
21.09.2015 / 16:42
1

I believe you are in doubt about recovering the form data. Try the code below. Model:

  public ActionResult Gravar()
  {
    string[] chaves = Request.QueryString.AllKeys;
    Dictionary parametros = new Dictionary();
    foreach (string chave in chaves)
    {
      parametros[chave] = Request.QueryString[chave];
    }

    string nome = parametros["Nome"];
    string tipo = parametros["Tipo"];
    string dia2 = parametros["Dia1"];
    string dia1 = parametros["Dia2"];
    string diaN = parametros["DiaN"];

    // demais linhas de código.

    return View();
  }
    
21.09.2015 / 16:47