Save multiple text box data with equal names

0

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

Today, registration works, but only one field only, and that's not what I want. I need to click on save to save all fields, generating different records in the table.

To get clearer, I have this screen:

Ihave12fieldsinthetotal,buteachrepresentsadifferentdatatype.

The"Type", "Schedule / Location" and "Food / Quantities" are fields where I have 6 types, schedules and locations and food and quantities ...

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

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

EDIT

For my failure I forgot to comment that this image is in a model that is a partial rendering inside a view master .. What happens is that I have a ViewModel and inside it several classes, including the one that represented in image.

    
asked by anonymous 31.05.2016 / 03:28

1 answer

1

To create a list of information you need to enumerate the View data. Example:

Class Template:

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'll only put 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 type box and so on with all input

Controller

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

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

When you send the form in ActionResult with List<Modelo> the variable model will be loaded with all 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 remain 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, property name Modelo , indexing element [0] ( [1] , [2] , etc.) and Field name of classe Modelo , summarizing Modelo[0].Tipo and so on. p>

Controller

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

Tip, it is often an exaggeration to use ViewModel , when you already have classes that can do the same role, anything that is too excessive in development, can cause problems at the time of maintenance, code repetition, etc. .

Perhaps this enumeration is best done before this issue by putting in your controler like this:

Controller

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