【发布时间】:2019-12-19 11:39:33
【问题描述】:
这里是新程序员,第一次发帖。我通常通过在 SO 或其他地方搜索来成功找到解决方案,但在这个问题上没有成功。我有一个使用 Microsoft .NET Framework 4.7 的 MVC 创建页面模板和一个使用 jquery-3.3.1.js 的模式实现。用户单击页面上的按钮以执行模式,在该模式中他们可以在表单保持可见的同时添加数据。当用户单击模式上的提交按钮时,我希望表单保留用户输入的数据或重新填充数据。目前,当用户在模式上单击“保存”时,它会从表单中删除数据。我可以使用 sessionStorage 成功存储页面中的数据,但我不能做的是在从模式中单击提交后重新填充页面。
我使用过 Chrome 和 Firefox 开发人员,可以在控制台中看到 sessionStorage。我将 getItem 行添加到 ajax 的成功参数中,并将其添加到 done 参数中。就重新填充表单而言,两者都没有成功。我在表单中添加了一个按钮,用户可以单击该按钮来检索他们的表单数据,但我无法使用该功能(业务需求)。
表单摘录 - 模式代码位于此末尾
@model HVAC.Models.EquipmentModel
@{
ViewBag.Title = "Create";
}
<h2>Add Equipment</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group col-md-6">
@Html.LabelFor(model => model.Customer_Name, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Customer_Name, new { htmlAttributes = new { @class = "form-control"} })
@Html.ValidationMessageFor(model => model.Customer_Name, "", new { @class = "text-danger" })
</div>
</div>
模态 - 此代码位于页面末尾
<div id="cylinder-add-modal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add Cylinder</h4>
</div>
@*<form>*@
<div class="modal-body">
<div class="alert alert-danger hide">
<strong>Error!</strong><span id="error"></span>
</div>
<div class="form-group">
<label for="Serial_Number" class="required">Serial Number:</label>
<input type="text" class="form-control" id="serialNumber">
<span id="serialNumber-error" class="text-danger hide">Serial Number is required</span>
</div>
<div class="modal-footer">
<button id="cylinder-add-modal-submit" type="button" class="btn btn-primary pull-left"><i class="fa fa-spinner fa-spin hide"></i> Save</button>
<button type="button" class="btn btn-default pull-right" data-dismiss="modal">Cancel</button>
</div>
@*</form>*@
</div>
</div>
</div>
保存会话数据 - 我能够在此处成功保存会话数据
j('#cylinder-add-modal-submit').on('click', function (e) {
console.log('adding cylinder');
sessionStorage.setItem('customer', j('#Customer_Name').val());
尝试检索会话数据并放回表单
j.ajax({
type: "POST",
url: siteRoot + "Test/CreateTest",
data: postData,
dataType: "json",
success: function (result) {
if (result.success) {
//alert(result.success)
modal.modal('hide');
console.log("inside success");
let customerdata = sessionStorage.getItem('customer');
console.log("proof of data " + customerdata);
j('#Customer_Name').val(customerdata);
} else {
modal.find('div.alert').removeClass('hide');
modal.find('#error').text(result.error);
}
}
})
.done(function () {
console.log("in done function");
console.log(customerdata);
j('#Customer_Name').val(customerdata);
//j('#Customer_Name').append(customerdata);
});
我只能用这个来检索它
j('#refresh').on('click', function (e) {
e.stopImmediatePropagation();
//j('#asset-add-modal').on('hidden.bs.modal', function () {
//alert('test');
//retrieve customer/equipment data
let customerdata = sessionStorage.getItem('customer');
我没有收到任何错误消息。我只是无法检索数据并将其重新填充到表单中。同样,表单是主页,当用户单击按钮添加数据时会弹出模式。我也尝试使用 .html 和 .text 来检索数据。
我试图在模态变为隐藏时检索数据,如代码所示(已注释掉)。
【问题讨论】:
标签: jquery forms post model-view-controller session-storage