我有一个相当简单的MVC应用程序,它从SQL数据库中提取数据,并使用footable来显示数据。一切都很好,而足球正如期而至。当我对表进行更改时,修改后的内容将显示在表视图中;但是,数据不会被发送回数据库。我对编程比较陌生,所以我不确定我要找的是ajax、反序列化器等等。我读过几篇试图解决这个问题的文章,但它们似乎不是我想要的,或者我不知道如何实现它们。我也不确定代码的哪些部分是必要的,以帮助解决这个问题,所以我将包括一些基本知识,但如果您需要更多的代码,请提供建议。
联合材料和视图/索引
< script >
var $modal = $('#editor-modal'),
$editor = $('form#editor'),
$editorTitle = $('#editor-title'),
ft = FooTable.init('#editing-example', {
editing: {
enabled: true,
addRow: function() {
$modal.removeData('row');
$editor[0].reset();
$editorTitle.text('Add a new row');
$modal.modal('show');
},
editRow: function(row) {
var values = row.val();
//$editor.find('CNTC_ID').val(parseInt(values.CNTC_ID));
$editor.find('#CNTC_ID').val(parseInt(values.CNTC_ID));
$editor.find('#CNTC_NM').val(values.CNTC_NM);
$editor.find('#WorkPhone').val(values.WorkPhone);
$editor.find('#CellPhone').val(values.CellPhone);
$editor.find('#Email').val(values.Email);
$editor.find('#Fax').val(values.Fax);
$editor.find('#IsFedContact').val(values.IsFedContact);
$editor.find('#ENTY_NM').val(values.ENTY_NM);
$editor.find('#TRBL_GOV_NM').val(values.TRBL_GOV_NM);
$modal.data('row', row);
$editorTitle.text('Edit row #' + parseInt(values.CNTC_ID));
$modal.modal('show');
},
deleteRow: function(row) {
if (confirm('Are you sure you want to delete the row?')) {
row.delete();
}
}
}
}),
uid = 10;
$editor.on('submit', function(e) {
if (this.checkValidity && !this.checkValidity()) return;
e.preventDefault();
var row = $modal.data('row'),
values = {
CNTC_ID: $editor.find('#CNTC_ID').val(),
CNTC_NM: $editor.find('#CNTC_NM').val(),
WorkPhone: $editor.find('#WorkPhone').val(),
CellPhone: $editor.find('#CellPhone').val(),
Email: $editor.find('#Email').val(),
Fax: $editor.find('#Fax').val(),
IsFedContact: $editor.find('#IsFedContact').val(),
ENTY_NM: $editor.find('#ENTY_NM').val(),
TRBL_GOV_NM: $editor.find('#TRBL_GOV_NM').val()
};
if (row instanceof FooTable.Row) {
row.val(values);
} else {
values.CNTC_ID = uid++;
ft.rows.add(values);
}
$modal.modal('hide');
});@model IEnumerable
<STAD_DEV.Models.STAD_CNTC>
@{ ViewBag.Title = "Index"; }
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table id="editing-example" class="table footable table-striped" data-paging="true" data-filtering="true" data-sorting="true" data-editing="true">
<thead>
<tr>
<th data-name="CNTC_ID">ID</th>
<th data-name="CNTC_NM" data-breakpoint="xs">Name</th>
<th data-name="WorkPhone">Work Phone</th>
<th data-name="CellPhone">Cell Phone</th>
<th data-name="Email">Email</th>
<th data-name="Fax">Fax</th>
<th data-type="html" data-name="IsFedContact">Is Fed Contact</th>
<th data-name="ENTY_NM">Department</th>
<th data-name="TRBL_GOV_NM">Tribe</th>
<th></th>
</tr>
</thead>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CNTC_ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.CNTC_NM)
</td>
<td>
@Html.DisplayFor(modelItem => item.WorkPhone)
</td>
<td>
@Html.DisplayFor(modelItem => item.CellPhone)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.Fax)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsFedContact)
</td>
<td>
@Html.DisplayFor(modelItem => item.GAIN_ST_GOV_ENTY.ENTY_NM)
</td>
<td>
@Html.DisplayFor(modelItem => item.GAIN_TRBL_GOV.TRBL_GOV_NM)
</td>
<td></td>
</tr>
}
</table>
<div class="modal fade" id="editor-modal" tabindex="-1" role="dialog" aria-labelledby="editor-title">
<div class="modal-dialog" role="document">
<form class="modal-content form-horizontal" id="editor">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="editor-title">Add Row</h4>
</div>
<div class="modal-body">
@*@<input type="number" id="CNTC_ID" name="CNTC_ID" class="hidden" />*@
<div class="form-group">
<label for="CNTC_ID" class="col-sm-3 control-label">CNTC_ID</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="CNTC_ID" name="CNTC_ID" placeholder="ID">
</div>
</div>
<div class="form-group required">
<label for="CNTC_NM" class="col-sm-3 control-label">Name</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="CNTC_NM" name="CNTC_NM" placeholder="Name" value="" required /> @*
<input type="text" class="form-control" id="CNTC_NM" name="CNTC_NM" placeholder="Name" required>*@
</div>
</div>
<div class="form-group">
<label for="WorkPhone" class="col-sm-3 control-label">WorkPhone</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="WorkPhone" name="WorkPhone" placeholder="Work Phone">
</div>
</div>
<div class="form-group">
<label for="CellPhone" class="col-sm-3 control-label">Cell Phone</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="CellPhone" name="CellPhone" placeholder="Cell Phone">
</div>
</div>
<div class="form-group">
<label for="Email" class="col-sm-3 control-label">Email</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="Email" name="Email" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="Fax" class="col-sm-3 control-label">Fax</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="Fax" name="Fax" placeholder="Fax">
</div>
</div>
<div class="form-group">
<label for="IsFedContact" class="col-sm-3 control-label">Is Fed Contact</label>
<div class="col-sm-9">
<select class="form-control" id="IsFedContact" name="IsFedContact">
<option value="True">yes</option>
<option value="False">no</option>
</select> @*
<input type="text" class="form-control" id="IsFedContact" name="IsFedContact" placeholder="Is Fed Contact">*@
</div>
</div>
<div class="form-group">
<label for="ENTY_NM" class="col-sm-3 control-label">Department</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="ENTY_NM" name="ENTY_NM" placeholder="">
</div>
</div>
<div class="form-group">
<label for="TRBL_GOV_NM" class="col-sm-3 control-label">Tribe</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="TRBL_GOV_NM" name="TRBL_GOV_NM" placeholder="">
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" data-trigger="ProcessData">Save changes</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</form>
</div>
</div>
我没有编辑后面的代码,也不确定是否需要,所以它只是自动生成的代码。我确信我输入的代码片段不会运行,但我仍然在学习堆栈溢出问题的适当格式。希望这一切都有意义。
发布于 2017-06-13 19:34:52
这个问题是由两个原因引起的,首先是ajax调用没有被指向正确的位置。第二个(实际上在post中没有提到)是由HTML将空格传递到表中造成的。这个问题的解决办法在这里找到:HTML Rendering Spaces
https://stackoverflow.com/questions/44118638
复制相似问题