2017-09-15 54 views
0

我正在制定時間表應用程序,其中員工根據管理員分配的項目數創建時間表。 This is a view of a user with 4 project to create a timesheet for. Each row here is a form created by a loop based on the number of projects passed to a viewbag.如何在ASP.NET MVC中提交未知數量的表單

這是我認爲的代碼創建的形式

<tbody style="font-size: 13px;"> 
         @{ 
          int i = 0; 

          } 
         @while (i < ViewBag.projCount) 
         { 
          using (Ajax.BeginForm("BillableHours", "TimesheetManager", 
           new AjaxOptions 
           { 
            OnSuccess = "OnSuccess", 
            OnFailure = "OnFailure", 
            LoadingElementId= "progress" 
           }, new { id= "Form-" + i})) 
          { 
          <tr> 
           <td> 
            <!-- Hours code begins --> 
            <div> 
             @Html.TextBoxFor(p => p.B_Monday, new { @class = "form-control", type = "number", min = "0", id = "B_Monday-" + i.ToString(), max = "8", onChange = "totalMonday(); checkTotal();" }) 
            </div> 
            <!-- hours code ends --> 
           </td> 
           <td> 
            <!-- Hours code begins --> 
            <div> 
             @Html.TextBoxFor(p => p.B_Tuesday, new { @class = "form-control", type = "number", min = "0", id = "B_Tuesday-" + i.ToString(), max = "8", onChange = "totalTuesday(); checkTotal();" }) 
            </div> 
            <!-- hours code ends --> 
           </td> 
           <td> 
            <!-- Hours code begins --> 
            <div> 
             @Html.TextBoxFor(p => p.B_Wednesday, new { @class = "form-control", type = "number", min = "0", id = "B_Wednesday-" + i.ToString(), max = "8", onChange = "totalWednesday(); checkTotal();" }) 
            </div> 
            <!-- hours code ends --> 
           </td> 
           <td> 
            <!-- Hours code begins --> 
            <div> 
             @Html.TextBoxFor(p => p.B_Thursday, new { @class = "form-control", type = "number", min = "0", id = "B_Thursday-" + i.ToString(), max = "8", onChange = "totalThursday(); checkTotal();" }) 
            </div> 
            <!-- hours code ends --> 
           </td> 
           <td> 
            <!-- Hours code begins --> 
            <div> 
             @Html.TextBoxFor(p => p.B_Friday, new { @class = "form-control", type = "number", min = "0", id = "B_Friday-" + i.ToString(), max = "8", onChange = "totalFriday(); checkTotal();" }) 
            </div> 
            <!-- hours code ends --> 
           </td> 
           <td> 
            <!-- Hours code begins --> 
            <div> 
             @Html.TextBoxFor(p => p.B_Saturday, new { @class = "form-control", type = "number", min = "0", id = "B_Saturday-" + i.ToString(), max = "8", onChange = "totalSaturday(); checkTotal();" }) 
            </div> 
            <!-- hours code ends --> 
           </td> 
           <td> 
            <!-- Hours code begins --> 
            <div> 
             @Html.TextBoxFor(p => p.B_Sunday, new { @class = "form-control", type = "number", min = "0", id = "B_Sunday-" + i.ToString(), max = "8", onChange = "totalSunday(); checkTotal();" }) 
            </div> 
            <!-- hours code ends --> 
           </td> 
           <td> 
            <!-- project selection --> 
            @Html.DropDownListFor(p => p.ProjectID, ViewBag.Projects as SelectList, "--select project--", new { @class = "form-control" }) 
            <!-- project selection ends --> 
           </td> 
           <td> 
            <!-- comments --> 
            <div> 
             @Html.TextBoxFor(p => p.ResourceComments, new { @class = "form-control", placeholder = "Comments...", type = "text" }) 
            </div> 

           </td> 
          </tr>        
          } 
          i++; 
         } 

        </tbody> 

請,我需要關於如何提交表單(S)的幫助下,考慮到行數是不同的每個用戶

這是視圖模型

public class BillableHoursViewModel 
{ 

    //billable hours 

    public int ProjectID { get; set; }   
    public double B_Monday { get; set; } 
    public double B_Tuesday { get; set; } 
    public double B_Wednesday { get; set; } 
    public double B_Thursday { get; set; } 
    public double B_Friday { get; set; } 
    public double B_Saturday { get; set; } 
    public double B_Sunday { get; set; } 
    public string ResourceComments { get; set; } 
} 
+0

做一個單一的表單,傳遞這個表單中的所有行並在控制器端排序它們會不會更容易? – Wndrr

+0

在您當前的示例中,每行的所有字段都不會具有相同的值嗎?你的ViewModel是什麼樣的? – Wndrr

+0

我已添加ViewModel –

回答

0

要發送多種形式這樣,你可以在每個窗體中添加一個隱藏的按鈕和程序「單擊」對T當你想要發送所有這些表格時,他按鈕。

的的程序化點擊可以使用$('.submitRow').click()來完成,考慮到你在包含submit類型與submitRow類的輸入每個窗體中添加一行。這裏有一個例子:

<tr> 
    <td> 
     <input type="submit" name="[email protected]" id="[email protected]" class="submitRow" style="display: none;" /> 
    </td> 
</tr> 

這將有效地發送給您的所有形式,但感覺就像你有一個問題,因爲所有的表示形式使用單一的視圖模型對象,所以他們都在相同的值你加載頁面,只有其中一個將被保存(否則你將在保存時出錯)。無論哪種方式,這似乎並不是你想要達到的。

您可能需要重新考慮將數據發送到視圖的方式。您當前的ViewModel適用於將數據從表單發送到視圖,但您需要一個列表來顯示從控制器發送的行。默認情況下,它`很難做到

<button type="button" class="buton_to_submit_all_forms">Submit all</button> 

在剃刀:

+0

考慮到每行有不同的項目ID,它們將不會有相同的值。但是的確如此,您的解決方案不會產生錯誤 –

0

您可以使用jQuery腳本:

$(".buton_to_submit_all_forms").one(function(){ 
    $(form).submit(); 
}); 

按鈕,你的頁面想要這個動作集。

+0

爲什麼在這裏使用「one」?如果他使用Ajax,則意味着該按鈕可能被多次點擊... – Wndrr

+0

此外,不會提交表單實際發送經典的HTTP POST請求以找到第一個發現的表單,並忽略其他表單,因爲該頁面已被「重定向「?我認爲'.submit()'不會使用Ajax調用。至少不是在我做的測試中: -/ – Wndrr

+1

加上,在$(form)中沒有聲明'form' var。我相信你的意思是'$('form')'? – Wndrr

相關問題