如何在ASP.NET MVC

时间:2017-09-15 12:54:55

标签: c# asp.net-mvc razor ajax.beginform

我正在处理时间表应用程序,其中员工根据管理员分配给他们的项目数创建时间表。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; }
 }

2 个答案:

答案 0 :(得分:0)

要发送多个这样的表单,您可以在每个表单中添加一个隐藏按钮,并以编程方式&#34;点击&#34;当你想要发送所有这些表格时,在按钮上。

可以使用$('.submitRow').click()完成程序化点击,考虑到您在每个表单中添加了一行,其中包含submit类型的输入submitRow类。这是一个例子:

<tr>
    <td>
        <input type="submit" name="submit_@i" id="submit_@i" class="submitRow" style="display: none;" />
    </td>
</tr>

这将有效地发送所有表单,但感觉您遇到问题,因为所有表示的表单都使用单个ViewModel对象,因此在加载页面时它们都将具有相同的值,并且只有其中一个将被保存(或者您在保存时会收到错误)。无论哪种方式,这似乎都不是你想要实现的目标。

您可能需要重新考虑将数据发送到视图的方式。您当前的ViewModel可以很好地将数据从表单发送到视图,但是您需要一个列表来显示从控制器发送的行。

答案 1 :(得分:0)

您可以使用jQuery脚本:

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

此操作的按钮设置在页面中您想要的位置:

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

默认情况下剃须刀很难做到。