Je travaille sur une application de feuille de temps où les employés créent une feuille de temps en fonction du nombre de projets auxquels ils ont été assignés par un administrateur. 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.Comment puis-je soumettre un nombre inconnu de formulaires dans ASP.NET MVC
C'est le code de mon point de vue qui crée les formes
<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'il vous plaît, je besoin d'aide sur la façon de soumettre le formulaire (s), compte tenu du nombre de lignes est différent pour chaque utilisateur
C'est le 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; }
}
Ne serait-il pas plus facile de créer un seul formulaire, de passer toutes les lignes de ce formulaire et de les trier du côté du contrôleur? – Wndrr
Dans votre exemple actuel, tous les champs de chaque ligne n'auront-ils pas la même valeur? À quoi ressemble votre ViewModel? – Wndrr
J'ai ajouté le ViewModel –