第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

將HTML表發(fā)布到ADO.NET DataTable

將HTML表發(fā)布到ADO.NET DataTable

肥皂起泡泡 2019-05-21 15:47:22
將HTML表發(fā)布到ADO.NET DataTable我的視圖中有一個(gè)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>我想遍歷所有html表行并在ADO.NET DataTable中插入值。簡(jiǎn)單來說,將HTML表轉(zhuǎn)換為ADO.NET DataTable。如何從HTML表中提取值并插入到ADO.NET DataTable中?該視圖基于以下模型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; }}
查看完整描述

2 回答

?
九州編程

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊

為了在回發(fā)時(shí)綁定到模型,name表單控件的屬性必須與模型屬性匹配。使用foreach循環(huán)不會(huì)生成正確的名稱屬性。如果您檢查html,您將看到多個(gè)實(shí)例


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

但為了綁定到您的模型,控件需要


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

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

考慮這個(gè)問題最簡(jiǎn)單的方法是考慮如何LeaveType在C#代碼中訪問屬性的值


var model = new LeaveBalanceViewModel();

// add some LeaveBalanceDetails instances to the LeaveDetailsList property, then access a value

var leaveType = model.LeaveDetailsList[0].LeaveType;

由于您的POST方法將具有參數(shù)名稱(例如model),只需刪除前綴(model),這就是控件的name屬性必須如何。為了做到這一點(diǎn),你必須使用一個(gè)for循環(huán)(集合必須實(shí)現(xiàn)IList<T>)


for(int i = 0; i < Model.LeaveDetailsList.Count; i++)

{

    @Html.TextBoxFor(m => m.LeaveDetailsList[i].LeaveType)

    ....

}

或使用自定義EditorTemplate(集合只需要實(shí)現(xiàn)IEnumerable<T>)


在 /Views/Shared/EditorTemplates/LeaveBalanceDetails.cshtml


@model yourAssembly.LeaveBalanceDetails

<tr>

    <td>@Html.TextBoxFor(m => m.LeaveType)</td>

    ....

</tr>

然后在主視圖中(不在循環(huán)中)


<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

}


查看完整回答
反對(duì) 回復(fù) 2019-05-21
  • 2 回答
  • 0 關(guān)注
  • 708 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)