Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将HTML表发布到ADO.NET DataTable_C#_Asp.net Mvc_Razor_Asp.net Mvc 5 - Fatal编程技术网

C# 将HTML表发布到ADO.NET DataTable

C# 将HTML表发布到ADO.NET DataTable,c#,asp.net-mvc,razor,asp.net-mvc-5,C#,Asp.net Mvc,Razor,Asp.net Mvc 5,我的视图中有一个HTML表,如下所示: <table id="tblCurrentYear"> <tr> <td>Leave Type</td> <td>Leave Taken</td> <td>Leave Balance</td> <td>Leave Total</td> </tr>

我的视图中有一个HTML表,如下所示:

<table id="tblCurrentYear">
    <tr>
        <td>Leave Type</td>
        <td>Leave Taken</td>
        <td>Leave Balance</td>
        <td>Leave Total</td>
    </tr>
    @foreach (var item in Model.LeaveDetailsList)
    {
        <tr>
            <td>@Html.TextBoxFor(m => item.LeaveType, new { width = "100" })</td>
            <td>@Html.TextBoxFor(m => item.LeaveTaken, new { width = "100" })</td>
            <td>@Html.TextBoxFor(m => item.LeaveBalance, new { width = "100" })</td>
            <td>@Html.TextBoxFor(m => item.LeaveTotal, new { width = "100" })</td>
        </tr>
    }
</table>

休假类型
请假
剩余余额
总假期
@foreach(Model.LeaveDetailsList中的var项)
{
@Html.TextBoxFor(m=>item.LeaveType,新的{width=“100”})
@Html.TextBoxFor(m=>item.leavetake,新的{width=“100”})
@Html.TextBoxFor(m=>item.LeaveBalance,新的{width=“100”})
@Html.TextBoxFor(m=>item.LeaveTotal,新的{width=“100”})
}
我希望遍历所有html表行,并在ADO.NET DataTable中插入值

简单地说,将HTML表转换为ADO.NET数据表

如何从HTML表中提取值并插入ADO.NET数据表

该视图基于以下模型

public class LeaveBalanceViewModel
{
    public LeaveBalanceViewModel()
    {
        this.EmployeeDetail = new EmployeeDetails();
        this.LeaveBalanceDetail = new LeaveBalanceDetails();
        this.LeaveDetailsList = new List<LeaveBalanceDetails>();
    }
    public EmployeeDetails EmployeeDetail { get; set; }
    public LeaveBalanceDetails LeaveBalanceDetail { get; set; }
    public List<LeaveBalanceDetails> LeaveDetailsList { get; set; }
}
公共类LeaveBalanceViewModel
{
公共假期余额视图模型()
{
this.EmployeeDetail=新的EmployeeDetails();
this.LeaveBalanceDetail=新的LeaveBalanceDetails();
this.LeaveDetailsList=新列表();
}
公共EmployeeDetail EmployeeDetail{get;set;}
public LeaveBalanceDetails LeaveBalanceDetail{get;set;}
公共列表LeaveDetailsList{get;set;}
}

为了绑定回发时的模型,表单控件的
名称
属性必须与模型属性匹配。使用
foreach
循环不会生成正确的名称属性。如果您检查html,您将看到

<input type="text" name="item.LeaveType" .../>
因为POST方法将有一个参数名(比如
model
),所以只需删除前缀(
model
),控件的name属性就必须是这样的。为此,必须使用
for
循环(集合必须实现
IList

然后在主视图中(不在循环中)


根据你的要求,试试这个

jQuery(document).on(“change”和“.ddlschoices”,函数(e){
var comma_choiceId='';
var comma_ChoicesText='';
$('input[class=“DDLChoices”]”)。每个(函数(e){
如果(选中此项){
逗号_choiceId=逗号_choiceId+$(this.val()+',';
逗号\选项文本=逗号\选项文本+$(this).parent('label').parent()+',';
}
});
$('#choiceId').val(逗号#choiceId);
$('#ChoiceText').val(逗号#ChoiceText);
});
@使用(Html.BeginForm(“Actionname”、“Controllername”、FormMethod.Post、new{id=“frmChoices”}))
{
@Html.HiddenFor(m=>m.ChoiceText,新的{@id=“ChoiceText”})
@HiddenFor(m=>m.choiceId,新的{@id=“choiceId”})
名称
挑选出来的
@foreach(@Model.Choices中的var项)
{
@item.ChoicesText
}

您正在寻找获取表的文本框值并插入到数据库中,还是需要插入完整的html?从我们对上一个问题的评论中是否不清楚,您不能使用
foreach
循环来生成集合中的控件。您需要
for
循环或自定义
EditorTemplate
模型。您的
foreach
loop不会绑定到任何东西。@StephenMuecke但foreach正在工作,我可以用它填充HTML表。生成此表后,用户可以更改任何文本框中的值,然后单击“保存”按钮。单击“保存”后,我想选择所有文本框值(逐行)并插入到ADO.NET DataTable中。不,它不是-您可以在视图中看到值,但回发时无法绑定任何内容。检查生成的html-您有多个带有
name=“LeaveType”
的文本框。要绑定回发时的集合,控件必须是
LeaveBalanceDetail[0].LeaveType
LeaveBalanceDetail[1].LeaveType
等。为什么需要将它们添加到ADO.Net表中?是否希望将它们保存到数据库中?这说明了前缀名称中必须包含哪些内容才能使razor引擎检测到列表。谢谢!;)这非常有用!不过,我仍然有一个问题-我将模型类型更改为列表,而不是IEnumerable,并迭代了thr在视图中使用For循环而不是For-Each循环。但是,当我提交时,它到达控制器,我的模型什么都没有!为什么会什么都没有?@Andarta,没有看到你的代码,我不知道你犯了什么错误。你需要问一个问题,显示相关细节,我们将能够回答:)
<input type="text" name="LeaveDetailsList[0].LeaveType" .../>
<input type="text" name="LeaveDetailsList[1].LeaveType" .../>
var model = new LeaveBalanceViewModel();
// add some LeaveBalanceDetails instances to the LeaveDetailsList property, then access a value
var leaveType = model.LeaveDetailsList[0].LeaveType;
for(int i = 0; i < Model.LeaveDetailsList.Count; i++)
{
    @Html.TextBoxFor(m => m.LeaveDetailsList[i].LeaveType)
    ....
}
@model yourAssembly.LeaveBalanceDetails
<tr>
    <td>@Html.TextBoxFor(m => m.LeaveType)</td>
    ....
</tr>
<table>
    .... // add headings (preferably in a thead element
    <tbody>
        @Html.EditorFor(m => m.LeaveDetailsList)
    </tbody>
</table>
public ActionResult Edit(LeaveBalanceViewModel model)
{
    // iterate over model.LeaveDetailsList and save the items
}