Domanda

The short and "straight" question:

A Controller action method (A) is called by a form with some parameters. In the same View, there is another form, which when submitted calls another action method (B) in the same controller. How can I access the values of (B) form (but essentially they are the content values of dropdowns and textboxes) from the action method A?

The Long and winding explanation:

I have a View with two forms, like this:

using (Html.BeginForm("List", "Rapporti"))
{
<span>Ricerca Rapporti per   Linea </span>

@Html.DropDownList("KLinea",
    new SelectList(new RapportiCT.Domain.Entities.RapportiEntities().TLinea, "KLinea", "DLinea")
    , "Scegli una linea");

<span>Data inizio</span>
@Html.TextBox("startDate", DateTime.Now.AddDays(-4).ToShortDateString(), new { @class = "dateTextBox" });                                                  

<span>Data fine</span>
@Html.TextBox("endDate", DateTime.Now.AddDays(-1).ToString("dd/MM/yyyy"), new { @class = "dateTextBox" })
<input type="submit" value="Cerca" />
}

if (Model.Count() > 0)
{
    var lastArticolo = "";
<h2>Rapporti Capoturno @Model.First().rapportoCT.DLinea</h2>
    <table>
    @foreach (var rapporto in Model)
    {
        if (lastArticolo != rapporto.rapportoCT.DCodArt)
        {
        <tr>
            <td class="td_title">
                @rapporto.rapportoCT.DCodArt
            </td>
            <td colspan="3" class="td_title">
                @rapporto.rapportoCT.DDescArt
            </td>
            <td class="td_title">
                Rendimento
            </td>
            <td class="td_title">
                #Pallet control.
            </td>
            <td class="td_title">
                #Pallet accanton.
            </td>
            <td class="td_title">
                Ore fermo linea
            </td>
        </tr>
            lastArticolo = rapporto.rapportoCT.DCodArt;
        }
        using (Html.BeginForm("ControlloQualita", "Rapporti"))
        {
        <tr>
            <td class="td_detail">
                @rapporto.rapportoCT.Data
            </td>
            <td class="td_detail">
                @rapporto.rapportoCT.Turno
            </td>
            <td class="td_detail">
                @rapporto.rapportoCT.Lettera
            </td>
            <td class="td_detail">
                @rapporto.rapportoCT.DNota
            </td>
            <td class="td_detail">
                @rapporto.RendimentoLinea
            </td>
            <td class="td_detail">
                @Html.TextBox("PalletControllati", rapporto.PalletControllati, new { style="width:50px" })
            </td>
            <td class="td_detail">
                @Html.TextBox("PalletAccantonati", rapporto.PalletAccantonati, new { style = "width:50px" })
            </td>
            <td class="td_detail">
                @Html.TextBox("OreFermoLinea", rapporto.OreFermoLinea, new { style = "width:50px" })
            </td>
            <td>
               <input type="submit" value="Salva" />
            </td>
        </tr>
        }
    }
</table>
}

else
{
@:Nessun record trovato
}

Both forms post to the same RapportiController: the first form is used to query the database and show the resulting records, with the second one I'd like to update the database record.

View (snapshot): enter image description here

So, my Controller class is like this:

// No problem here
public ViewResult List(int? KLinea = null, DateTime? startDate = null, DateTime? endDate = null)
    {
        IQueryable<VRapportiCT> qVRapporti = repository
                    .ViewRapporti(KLinea.Value, startDate.Value, endDate.Value);

        List<VRapportiCT> lRapporti = qVRapporti.ToList();

        List<RapportoRendimentoViewModel> listRRVM = new List<RapportoRendimentoViewModel>();
        foreach (var rapporto in lRapporti)
        {
            RapportoRendimentoViewModel rrVM = new RapportoRendimentoViewModel();
            rrVM.rapportoCT = rapporto;
            rrVM.RendimentoLinea = repository.GetRendimentoLinea(rapporto);
            rrVM.PalletControllati = "0";
            rrVM.PalletAccantonati = "0";
            rrVM.OreFermoLinea = "0";
            listRRVM.Add(rrVM);
        }

        return View(listRRVM);
    }

    public RedirectToRouteResult ControlloQualita(int PalletControllati, int PalletAccantonati, int OreFermoLinea)
    {
        // How can I access the 1°form values here (a.k.a. the DropDown and Date text box values?

        // ...Fake values, only to assert that the parameters get passed to the action...
        RouteValueDictionary dic = new RouteValueDictionary();
        dic.Add("KLinea", 55);
        dic.Add("startDate", DateTime.Now.AddDays(-4));
        dic.Add("endDate", DateTime.Now);

        return RedirectToAction("List", "Rapporti", dic);
    }
È stato utile?

Soluzione

I see 2 options here:

  1. Store last submitted "List" form in, lets say, Session, or TempData, or any other storage you like and restore these values on "ControlloQualita" form submission. This approch assumes that you need last submitted values from "List" form rather than latest.

  2. Intercept "ControlloQualita" form submission in order to append values of "List" form to the request. This can be achieved with javascript by adding hidden inputs to the "ControlloQualita" form.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top