短而“直”的问题:
控制器动作方法(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);
}
答案 0 :(得分:0)
我在这里看到两个选项:
将最后提交的“列表”表单存储在,例如Session,TempData或您喜欢的任何其他存储中,并在“ControlloQualita”表单提交中恢复这些值。此approch假定您需要从“列表”表单而不是最新提交的最后提交的值。
拦截“ControlloQualita”表单提交,以便将“List”表单的值附加到请求中。这可以通过在“ControlloQualita”表单中添加隐藏输入来实现。