1

简短而“直接”的问题:

控制器操作方法 (A) 由带有一些参数的表单调用。在同一个视图中,还有另一个表单,它在提交时会调用同一个控制器中的另一个操作方法(B)。如何从操作方法 A 访问 (B) 表单的值(但本质上它们是下拉列表和文本框的内容值)?

长而曲折的解释:

我有两种形式的视图,如下所示:

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
}

两个表单都发布到同一个RapportiController:第一个表单用于查询数据库并显示结果记录,第二个表单我想更新数据库记录。

查看(快照): 在此处输入图像描述

所以,我的 Controller 类是这样的:

// 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);
    }
4

1 回答 1

1

我在这里看到 2 个选项:

  1. 将上次提交的“列表”表单存储在 Session 或 TempData 或您喜欢的任何其他存储中,并在“ControlloQualita”表单提交时恢复这些值。此方法假定您需要“列表”表单中最后提交的值,而不是最新的。

  2. 拦截“ControlloQualita”表单提交,以便将“List”表单的值附加到请求中。这可以通过向“ControlloQualita”表单添加隐藏输入来使用 javascript 来实现。

于 2013-11-04T18:11:09.147 回答