2013-11-04 109 views
0

值短和“直”的问题:MVC从另一种形式中获取相同的视图

一个控制器的动作方法,(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:第一种形式是用来查询数据库并显示结果记录,第二个我想更新数据库记录。

视图(快照): enter image description here

所以,我的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

使用web,在每次新提交时,前一个提交中的变量都会丢失。你必须找到一种方法来存储它们(例如:会话,隐藏的html标签,查询字符串,数据库,缓存) –

回答

0

我看到2个选项在这里:

  1. 储存最后提交的 “清单”可以说,Session,或TempData或任何其他您喜欢的存储,并在“ControlloQualita”表单提交中恢复这些值。这种认识假定你需要最后提交的“列表”形式的值而不是最新的。

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