HTML表到ADO.NET数据表 [英] HTML Table to ADO.NET DataTable

查看:193
本文介绍了HTML表到ADO.NET数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML表下面笔者认为:

 <表ID =tblCurrentYear>
    &所述; TR>
        < TD>保留类型< / TD>
        < TD>放取< / TD>
        < TD>留余量< / TD>
        < TD>保留总计< / TD>
    < / TR>
    @foreach(在Model.LeaveDetailsList VAR项)
    {
        &所述; TR>
            < TD> @ Html.TextBoxFor(M = GT; item.LeaveType,新{WIDTH =100})LT; / TD>
            < TD> @ Html.TextBoxFor(M = GT; item.LeaveTaken,新{WIDTH =100})LT; / TD>
            < TD> @ Html.TextBoxFor(M = GT; item.LeaveBalance,新{WIDTH =100})LT; / TD>
            < TD> @ Html.TextBoxFor(M = GT; item.LeaveTotal,新{WIDTH =100})LT; / TD>
        < / TR>
    }
< /表>

我想通过所有的HTML表行迭代和ADO.NET数据表中插入值。

简单的讲,HTML表格转换为ADO.NET数据表。

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

该视图是基于下面的模型

 公共类LeaveBalanceViewModel
{
    公共LeaveBalanceViewModel()
    {
        this.EmployeeDetail =新EmployeeDetails();
        this.LeaveBalanceDetail =新LeaveBalanceDetails();
        this.LeaveDetailsList =新的List< LeaveBalanceDetails>();
    }
    公共EmployeeDetails EmployeeDetail {搞定;组; }
    公共LeaveBalanceDetails LeaveBalanceDetail {搞定;组; }
    公开名单< LeaveBalanceDetails> LeaveDetailsList {搞定;组; }
}


解决方案

为了绑定到一个模型中的回发,表单控件的名称属性必须匹配模型属性。您使用的foreach 循环不会产生正确的名称属性。如果你检查HTML,您将看到的多个实例

 <输入类型=文本名称=LeaveType... />

但为了结合到模型中的控制将需要

 <输入类型=文本名称=LeaveDetailsList [0] .LeaveType... />
<输入类型=文本名称=LeaveDetailsList [1] .LeaveType... />

等。考虑最简单的方法是考虑你将如何访问 C# $ C一 LeaveType 属性的值$ C

  VAR模型=新LeaveBalanceViewModel();
//添加一些LeaveBalanceDetails实例的LeaveDetailsList属性,然后访问值
变种leaveType = model.LeaveDetailsList [0] .LeaveType;

由于您的POST方法将有一个参数名称(例如模式),只是下降了preFIX(模式),这就是控制的name属性必须如何。为了做到这一点,你必须使用一个循环(集合必须落实的IList< T>

 的for(int i = 0; I< Model.LeaveDetailsList.Count;我++)
{
    @ Html.TextBoxFor(M = GT; m.LeaveDetailsList [I] .LeaveType)
    ....
}

或使用自定义的 EditorTemplate (集合只需执行的IEnumerable< T>

/View/Shared/EditorTemplates/LeaveBalanceDetails.cshtml

  @model yourAssembly.LeaveBalanceDetails
&所述; TR>
    < TD> @ Html.TextBoxFor(M = GT; m.LeaveType)LT; / TD>
    ....
< / TR>

和然后在主视图中(而不是在一个循环中)

 <表>
    .... //在THEAD元素添加标题(preferably
    <&TBODY GT;
        @ Html.EditorFor(M = GT; m.LeaveDetailsList)
    < / TBODY>
< /表>

和最后,在控制器

 公众的ActionResult编辑(LeaveBalanceViewModel模型)
{
    //迭代model.LeaveDetailsList和保存项目
}

I have a HTML table as below in my View:

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

I want to iterate through all the html table rows and insert the values in ADO.NET DataTable.

Simple speaking, converting HTML Table to ADO.NET DataTable.

How to extract values from HTML Table and insert into ADO.NET DataTable?

The view is based on the following model

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

解决方案

In order to bind to a model on post back, the name attributes of the form controls must match the model properties. Your use of a foreach loop does not generate the correct name attributes. If you inspect the html you will see multiple instances of

<input type="text" name="LeaveType" .../>

but in order to bind to your model the controls would need to be

<input type="text" name="LeaveDetailsList[0].LeaveType" .../>
<input type="text" name="LeaveDetailsList[1].LeaveType" .../>

etc. The easiest way to think about this is to consider how you would access the value of a LeaveType property in C# code

var model = new LeaveBalanceViewModel();
// add some LeaveBalanceDetails instances to the LeaveDetailsList property, then access a value
var leaveType = model.LeaveDetailsList[0].LeaveType;

Since your POST method will have a parameter name (say model), just drop the prefix (model) and that's how the name attribute of the control must be. In order to do that you must use either a for loop (the collection must implement IList<T>)

for(int i = 0; i < Model.LeaveDetailsList.Count; i++)
{
    @Html.TextBoxFor(m => m.LeaveDetailsList[i].LeaveType)
    ....
}

or use a custom EditorTemplate (the collection need only implement IEnumerable<T>)

In /View/Shared/EditorTemplates/LeaveBalanceDetails.cshtml

@model yourAssembly.LeaveBalanceDetails
<tr>
    <td>@Html.TextBoxFor(m => m.LeaveType)</td>
    ....
</tr>

and then in the main view (not in a loop)

<table>
    .... // add headings (preferably in a thead element
    <tbody>
        @Html.EditorFor(m => m.LeaveDetailsList)
    </tbody>
</table>

and finally, in the controller

public ActionResult Edit(LeaveBalanceViewModel model)
{
    // iterate over model.LeaveDetailsList and save the items
}

这篇关于HTML表到ADO.NET数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆