【发布时间】:2018-04-05 19:16:31
【问题描述】:
我目前正在尝试通过 HTML 表单插入模型对象,但是在调试时,它是作为空对象的第二个表,即使我指定的输入字段的名称属性与模型中调用它们的方式相匹配。
这是我的表的 edmx 图:
我的模特:
public class TReportHeaderModel
{
public int ID { get; set; }
public int ClientID { get; set; }
public string THeaderTitle { get; set; }
public int RowNumber { get; set; }
public IList<TReporURLModel> TotalReports { get; set; }
}
public class TReporURLModel
{
public int ID { get; set; }
public string name { get; set; }
public string url { get; set; }
public int RowNumber { get; set; }
public string hash { get; set; }
//public int THeaderID { get; set; }
}
这是我作为模态的 HTML 表单。请忽略 JavaScript,因为它是一种演示我在这里尝试实现的表单功能的方式。
但是,我提交表单的ajax如下:
initCreateGroupForm: function ()
{
$('#create-report-group-form').ajaxForm({
dataType: 'json',
beforeSerializate: function ($form, options) {
Reporting.Forms = $form;
},
success: function (response) {
if (response.Success == true) {
JQueryUX.Msg.BootStrapShow({
msg: response.Message,
className: 'alert alert-success',
title: 'Report Has Been Created.'
});
$("#create-report-group-modal").modal('hide');
}
else {
alert(response.Message);
}
},
onError: function (xhr, status, error)
{
alert(error);
}
});
$(document).ready(function() {
$("#add_row").on("click", function() {
// Dynamic Rows Code
// Get max row id and set new id
var newid = 0;
$.each($("#tab_logic tr"), function() {
if (parseInt($(this).data("id")) > newid) {
newid = parseInt($(this).data("id"));
}
});
newid++;
var tr = $("<tr></tr>", {
id: "addr" + newid,
"data-id": newid
});
// loop through each td and create new elements with name of newid
$.each($("#tab_logic tbody tr:nth(0) td"), function() {
var cur_td = $(this);
var children = cur_td.children();
// add new td and element if it has a nane
if ($(this).data("name") != undefined) {
var td = $("<td></td>", {
"data-name": $(cur_td).data("name")
});
var c = $(cur_td).find($(children[0]).prop('tagName')).clone().val("");
c.attr("name", $(cur_td).data("name") + newid);
c.appendTo($(td));
td.appendTo($(tr));
} else {
var td = $("<td></td>", {
'text': $('#tab_logic tr').length
}).appendTo($(tr));
}
});
// add delete button and td
/*
$("<td></td>").append(
$("<button class='btn btn-danger glyphicon glyphicon-remove row-remove'></button>")
.click(function() {
$(this).closest("tr").remove();
})
).appendTo($(tr));
*/
// add the new row
$(tr).appendTo($('#tab_logic'));
$(tr).find("td button.row-remove").on("click", function() {
$(this).closest("tr").remove();
});
}); <!--Ends Here-->
$("#add_row").trigger("click");
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form-horizontal" id="create-report-group-form" action="@Url.Action(" CreateReport ", "TReporting ")" method="post">
<input type="hidden" id="hidden-report-group-id" name="ID" value="null" />
<div class="col-lg-12 table-responsive">
<div class="col-lg-12" style="margin-bottom:20px;margin-left:-18px;">
<label> Report Group Title</label>
<input type="text" class="form-control" id="input-report-header-title" placeholder="Report Group Title" name="THeaderTitle">
</div>
<table class="table table-bordered table-hover table-sortable" id="tab_logic">
<thead>
<tr>
<th class="text-center">
Report Name
</th>
<th class="text-center">
URLs
</th>
<th>
<button id="add_row" type="button" class="btn btn-success" data-role="add">
<span class="glyphicon glyphicon-plus"></span>
</button>
</th>
</tr>
</thead>
<tbody>
<tr id='addr0' data-id="0" class="">
<td data-name="name">
<input type="hidden" id="hidden-report-url-id" name="ID" value="null" />
<input type="text" id="input-report-name" name='name' placeholder='Report Name' class="form-control" />
</td>
<td data-name="url">
<input type="text" id="input-report-url" name='url' placeholder='https://' class="form-control" />
</td>
<td data-name="del">
<button name="del0" type="button" class='btn btn-danger glyphicon glyphicon-remove row-remove'></button>
</td>
</tr>
</tbody>
</table>
<div class="modal-footer">
<button type="submit" class="btn-save btn btn-primary btn-block">Save</button>
<button type="button" class="btn-cancel btn btn-secondary btn-block">Cancel</button>
</div>
<!--Modal-Footer Ends Here-->
</div>
<!---Model Body Ends Here-->
</form>
是否有特定的方法来调用属于第二个表的输入,以便输入可以毫无问题地传递到 TReport 表。
因为无论我做什么,它都会以 null 的形式返回到服务中。当我调试它时,我将获得第一个表的输入,但没有任何内容会传递给第二个查询,即 TReport 表。我的服务代码如下:
public void CreateReport(TReportHeaderModel model)
{
using (var connection = new TReportEntitiesConnection())
{
connection.THeader.Add(new THeader()
{
ID = model.ID,
THeaderTitle = model.THeaderTitle,
RowNumber = model.RowNumber
});
foreach (var urls in model.TotalReports)
{
connection.TReport.Add(new TReport()
{
TReportName=urls.name,
URL=urls.url
});
}
connection.SaveChanges();
}
谢谢!
【问题讨论】:
-
您没有创建具有正确
name属性的输入,尽管您认为它们需要是name="TotalReports[0].ID"、name="TotalReports[1].ID"等(请参阅 this answer 以获得解释 -
您似乎想要动态创建(和删除)新的收藏项,在这种情况下,请参阅this answer
-
@StephenMuecke 嗨斯蒂芬,当我尝试使用 for 循环进行循环时,它仍然给我空结果。你能告诉我一些正确处理这种方法的例子吗?
-
看看我给你的两个链接,特别是第二个,因为你似乎想要动态添加收藏项
-
@GerleBatde 仅供参考,我在回答中包含了重新索引,请告诉我它对您的工作原理。
标签: jquery asp.net-mvc linq-to-sql linq-to-entities ajaxform