2017-09-15 6 views
0

私は、従業員が管理者によって割り当てられたプロジェクトの数に基づいてタイムシートを作成するタイムシートアプリケーションで作業しています。ASP.NET MVCで未知数のフォームを提出するには

この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.は、行の数は、ユーザ毎に異なる考慮すると、

<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> 

が、私はフォーム(複数可)を提出する方法についての助けを必要としてくださいフォームを作成し、私の見解からのコードです

これは、このような複数のフォームを送信するにはViewModelに

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

である、あなたは、各フォームに隠しボタンを追加することができますし、プログラムトンの「クリック」これらのフォームをすべて送信する場合は、ボタンをクリックします。

programmaticalクリックすると、submitRowクラスでsubmitタイプの入力を含む各フォームの行を追加することを考慮すると、$('.submitRow').click()を用いて行うことができます。次に例を示します。

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

これは効果的にあなたのフォームの全てを送信しますが、言った形態のすべてが単一のViewModelオブジェクトを使用するので、あなたが問題を抱えているので、彼らはすべての時に同じ値を持つことになりますようにそれは感じていますページを読み込み、そのうちの1つだけが保存されます(または保存中にエラーが発生します)。いずれにせよ、これは達成しようとしているものではないようです。

ビューにデータを送信する方法を再考する必要があります。現在のViewModelは、フォームからビューにデータを送信するのにうまく機能しますが、コントローラーから送信された行を表示するにはそれらのリストが必要です。あなたがページにしたい、このアクションセットのため

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

ボタン:

+0

それぞれの行に異なるプロジェクトIDがあるため、同じ値を持つことはありません。しかし、実際には、あなたのソリューションはエラーを生成しません –

0

あなたはjQueryのスクリプトを使用することができますかみそりで

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

を行うことは非常に難しいデフォルトそれ `sで。

+0

なぜここで "one"を使用するのですか?彼がAjaxを使用している場合、ボタンが複数回クリックされる可能性があります。 – Wndrr

+0

また、フォームを送信しても、最初に見つかったフォームの古典的なHTTP POSTリクエストは送信されず、ページがリダイレクトされているので、 "? '.submit()'はAjax呼び出しを使わないと思う。少なくとも私が行ったテストではない: -/ – Wndrr

+1

さらに '$(form)' 'form' varは宣言されていません。私はあなたが '$( 'form')'を意味すると信じていますか? – Wndrr

関連する問題