【发布时间】:2018-01-18 06:04:27
【问题描述】:
下面是我的 Razor 视图
<tr id="CGT_Row">
<td class="noGutter">@Html.TextBoxFor(m => m.clsCGT.CGT_Visit_Date, new {@id="dp1", @class = "input-sm text-center date", @readonly = "true" }) </td>
<td class="noGutter">
@Html.DropDownListFor(m => m.clsCGT.days_of_CGT, new List<SelectListItem>
{
new SelectListItem { Text="1 Day",Value="1 Day"},
new SelectListItem { Text="2 Day",Value="2 Day"},
new SelectListItem { Text="3 Day",Value="3 Day"},
},
new { @class = "input-sm", @actor = "DropDown" })
</td>
<td class="noGutter">@Html.TextBoxFor(m => m.clsCGT.village_Name, new { @class = "input-sm text-center" })</td>
<td class="noGutter">@Html.DropDownListFor(m => m.clsCGT.fo_Name_CGT, TempData["Staff_List"] as IEnumerable<SelectListItem>, new { @id = "ddl_CGT_fo", @class = "input-sm", @actor = "DropDown", @style = "width:auto;" })</td>
<td class="noGutter">
@Html.DropDownListFor(m => m.clsCGT.member_attendence_CGT, new List<SelectListItem>
{
new SelectListItem { Text="Less than 100%",Value="Less than 100%"},
new SelectListItem { Text="100%",Value="100%"}
},
new { @class = "input-sm", @actor = "DropDown", @style = "width:auto;" })
</td>
<td class="noGutter">
@Html.DropDownListFor(m => m.clsCGT.process_follow_CGT, new List<SelectListItem>
{
new SelectListItem { Text="n",Value="n"},
new SelectListItem { Text="y",Value="y"}
},
new { @class = "input-sm", @actor = "DropDown", @style = "width:100%;" })
</td>
<td class="noGutter">
@Html.DropDownListFor(m => m.clsCGT.CGT_timing, new List<SelectListItem>
{
new SelectListItem { Text="As per time",Value="As per time"},
new SelectListItem { Text="Delayed",Value="Delayed"},
new SelectListItem { Text="Reschedule",Value="Reschedule"},
},
new { @class = "input-sm", @actor = "DropDown", @style = "width:auto;" })
</td>
<td class="noGutter">
@Html.DropDownListFor(m => m.clsCGT.fo_comm_to_client, new List<SelectListItem>
{
new SelectListItem { Text="n",Value="n"},
new SelectListItem { Text="y",Value="y"}
},
new { @class = "input-sm", @actor = "DropDown", @style = "width:100%" })
</td>
<td class="noGutter">
@Html.DropDownListFor(m => m.clsCGT.member_house_verification_CGT, new List<SelectListItem>
{
new SelectListItem { Text="n",Value="n"},
new SelectListItem { Text="y",Value="y"}
},
new { @class = "input-sm", @actor = "DropDown", @style = "width:100%;" })
</td>
<td class="noGutter">
@Html.DropDownListFor(m => m.clsCGT.documentation_complete_CGT, new List<SelectListItem>
{
new SelectListItem { Text="n",Value="n"},
new SelectListItem { Text="y",Value="y"}
},
new { @class = "input-sm", @actor = "DropDown", @style = "width:100%;" })
</td>
<td class="noGutter">
@Html.DropDownListFor(m => m.clsCGT.CGT_conducted_for_3_days, new List<SelectListItem>
{
new SelectListItem { Text="n",Value="n"},
new SelectListItem { Text="y",Value="y"}
},
new { @class = "input-sm", @actor = "DropDown", @style = "width:100%;" })
</td>
<td class="noGutter">@Html.TextAreaFor(m => m.clsCGT.CGT_remarks, new { @class = "input-sm" })</td>
</tr>
这是与模型绑定并单击按钮我可以毫无问题地将其保存到数据库中。
但我需要在将其保存到数据库之前添加另一行
所以我使用jquery在该行下方的表格中添加另一行
function Create_New_Row(id) {
var row; var table;
var closeBtn = '<td><a onclick="$(this).parent().parent().remove();"><i class="fa fa-times fa-lg fa-border" aria-hidden="true" style="color:#e90029"></i></a></td>';
if (id == 'CGT_Row')
{
row = '#CGT_Row';
table = '#tbl_CGT tr:last';
}
else if (id == 'GRT_Row')
{
row = '#GRT_Row';
table = '#tbl_GRT tr:last';
}
else if (id == 'Disb_Row')
{
row = '#Disb_Row';
table = '#tbl_Disb tr:last';
}
var v = $(row);
var html = '<tr>' + v.html() + closeBtn + '</tr>';
$(table).after(html);
$('[id^="dp"]').datepicker({
format: 'dd/mm/yyyy',
autoclose: true
});
}
这看起来像这样
点击添加新行 - 将新行添加到表格中
保存详细信息 - 将所有这些行保存到数据库中
问题是 - 只有第一行绑定到模型,我可以访问它以保存数据,如何从其他行获取数据(稍后添加)因为它们只是第一行 html 的复制副本,只有没有模型绑定
例如 - 如果我填写了 4 行而不是单击“保存详细信息”,则所有 4 行都应保存在数据库(SQL Server)中
更新:1
给维克多·奥列克西辛
下面是我将这个表值转换为数组并将其传递给控制器的 js 代码
function Table_to_Array(id) {
if (id == "CGT_Data")
{ var tbl="tbl_CGT"; }
if (id == "GRT_Data")
{ var tbl = "tbl_GRT"; }
if (id == "Disb_Data")
{ var tbl = "tbl_Disb"; }
var myTableArray = $('#'+tbl+' tr').map(function () {
return $(this).find(':input').map(function () {
return this.value;
}).get();
}).get();
return myTableArray;
}
function send_to_server(id)
{
var iden = id;
var result = Table_to_Array(id);
var data = {
data_holder: result,
Category: id
};
var params = {
url: '@Url.Action("Send_to_server", "Annex1")',
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
traditional : true,
data: JSON.stringify(data),
success: function (result) { alert('Ok! It worked.'); },
error: function (result) { alert('Warning! It failed.'); }
};
$.ajax(params);
}
它确实将数组传递给控制器,但也显示警报失败 - (设置错误:function [alert('Warning! It failed.'); ])
【问题讨论】:
-
你用ajax保存吗?
-
我认为使用jQuery动态生成表格是相当复杂的。为 AJAX 调用(使用索引)添加的每一行渲染的单个局部视图和使用自定义模型绑定器来保存行数据比完全 JS/jQuery 解决方案更可取。
-
@TetsuyaYamamoto 你能推荐任何关于这种方法的帖子吗?
-
@PowerStar ,没有。目前我不知道保存这些数据。在 Asp.net 中使用 gridview 是可行的,它允许我们添加页脚模板功能,但我不知道如何在 mvc 中做到这一点
-
您的 post 方法是从 jquery ajax 还是从服务器端本身处理的?如果可以,请分享。
标签: javascript c# jquery asp.net-mvc asp.net-mvc-5