Save multiple data from text boxes with equal names

Asked

Viewed 98 times

0

In my project I have a rule that is to save several fields with the same name at once.

Today, the registration works, but only one field, and that’s not what I want.. I need that when clicking to save, all fields are saved, generating records in different table.

To make it clearer, I have this screen: inserir a descrição da imagem aqui

I have 12 fields in total, but each one represents a different type of data.

The "Type", "Hourly/Local" and "Food/Quantities" are fields where I have 6 types, times and places and food and quantities...

I need to save everything this way, all the data at once... How can I do this?

More or less the style of this question here, the difference is that I use Asp.net mvc instead of PHP.

EDIT

By my fault I forgot to comment that, this image stays on a model that is a partial being rendered inside a view master.. What happens is that I have a Viewmodel and within it several classes, including the one represented in the image.

  • I didn’t understand different fields!

  • They are the same fields, only replicated several times. In my table I only have 3 columns, I want to replicate in the view and save everything...

  • but, you need to tell if in View it will have to create the fields or will be 12 lines as default ?

  • The twelve lines as standard

  • Maybe so, but I couldn’t understand how it works... @Ciganomorrisonmendez

  • It seems to me that there are not necessarily 12 fields. It may be more or less. If so, it is the case of Begincollectionitem. So I would still like one more answer?

  • Yes, yes @Ciganomorrisonmendez, no problem!

Show 3 more comments

1 answer

1


To create an information list you need to list the View data. Example:

Class model:

using System;
namespace WebApplication10.Models
{
    public class Modelo
    {
        public string Tipo { get; set; }
        public DateTime DataHora { get; set; }
        public int AlimentosQuantidades { get; set; }
    }
}

Fixed view with 12 rows and 3 columns

@using (Html.BeginForm())
{
    for (int i = 0; i < 12; i++)
    {
        <div class="row">

            <div class="col-md-4">
                @Html.TextBox(string.Format("[{0}].Tipo", i), null, new { @class = "form-control" })
            </div>
            <div class="col-md-4">
                @Html.TextBox(string.Format("[{0}].DataHora", i), null, new { @class = "form-control" })
            </div>
            <div class="col-md-4">
                @Html.TextBox(string.Format("[{0}].AlimentosQuantidades", i), null, new { @class = "form-control" })
            </div>
        </div>
    }
    <button>Enviar</button>
}

The code generated by this View (I will put only 2 lines)

<div class="col-md-4">
        <input class="form-control" name="[0].Tipo" type="text" value="" />
    </div>
    <div class="col-md-4">
        <input class="form-control" name="[0].DataHora" type="text" value="" />
    </div>
    <div class="col-md-4">
        <input class="form-control" name="[0].AlimentosQuantidades" type="text" value="" />
    </div>
</div>
<div class="row">

    <div class="col-md-4">
        <input class="form-control" name="[1].Tipo" type="text" value="" />
    </div>
    <div class="col-md-4">
        <input class="form-control" name="[1].DataHora" type="text" value="" />
    </div>
    <div class="col-md-4">
        <input class="form-control" name="[1].AlimentosQuantidades" type="text" value="" />
    </div>
</div>

So with [0].Tipo is the first box and [1].Tipo is the next box of the kind and so on with all the input

Controller

[HttpGet()]
public ActionResult Lista()
{
    return View();
}

[HttpPost()]
public ActionResult Lista(List<Modelo> model)
{
    return RedirectToAction("Lista");
}

When you send the form to ActionResult with List<Modelo> the variable model will be loaded with all the information

EDIT

When you have a ViewModel and a property that represents this field listing in your View put the name of the property and the rest remains the same.

Example:

Viewmodel

using System;

namespace WebApplication10.Models
{
    public class ViewModel
    {
        public List<Modelo> Modelo {get;set;}
        //... os outros itens.
    }
}

View

@using (Html.BeginForm())
{
    for (int i = 0; i < 12; i++)
    {
        <div class="row">

            <div class="col-md-4">
                @Html.TextBox(string.Format("Modelo[{0}].Tipo", i), null, new { @class = "form-control" })
            </div>
            <div class="col-md-4">
                @Html.TextBox(string.Format("Modelo[{0}].DataHora", i), null, new { @class = "form-control" })
            </div>
            <div class="col-md-4">
                @Html.TextBox(string.Format("Modelo[{0}].AlimentosQuantidades", i), null, new { @class = "form-control" })
            </div>
        </div>
    }
    <button>Enviar</button>
}

That is, name of the property Modelo, indexation of the element [0] ([1], [2] , etc.) and Name of the classe Modelo, summing up Modelo[0].Tipo and so on.

Controller

[HttpPost()]
public ActionResult Lista(ViewModelExample example)
{
    return RedirectToAction("Lista");
}

Tip, it is an exaggeration often the use of ViewModel, when there are already classes that can play the same role, everything that is too much in development, can cause problems at the time of maintenance, code repetition, etc.

Maybe this enumeration is best done before this issue by putting in your controller like this:

Controller

[HttpPost()]
public ActionResult Lista(List<Modelo> model, ViewModelExample example)
{
    return RedirectToAction("Lista");
}

Browser other questions tagged

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