我希望打开并填充一个Bootstrap模式,基于从我的HTML表格中选择的行和我从模型中获得的数据,我的HTML表格中有一个按钮,它将调用JS函数并传递ID到它。我能够恢复数据,但我不知道如何使用JSON列表填充模型。
这是我想要动态构建的模态:
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Change User Deparment</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="Employee Number" class="form-control-label">Employee Number:</label>
<input type="text" class="form-control" id="Employee Number">
</div>
<div class="form-group">
<label for="userName" class="form-control-label">User Name:</label>
<input type="text" class="form-control" id="userName">
</div>
<label for="department">Select Department</label>
<select class="form-control" id="department">
<option>Option1</option>
<option>Option2</option>
<option>Option3</option>
</select>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Send message</button>
</div>
</div>
</div>
</div>
这是我的JS函数,用于从我的模型中获取数据。
function editUser(id) {
//alert(userId);
$.ajax({
type: "GET",
url: '@Url.Action("GetUserDetails", "Admin")',
contentType: "application/json; charset=utf-8",
datatype: JSON,
data: ({ 'Value': id }),
success: function () {
},
error: function () {
alert(test);
}
});
}
答案 0 :(得分:0)
首先,您可能希望为模式中的输入字段指定名称:
<div class="modal-body">
<form>
<div class="form-group">
<label for="Employee Number" class="form-control-label">Employee Number:</label>
<input type="text" class="form-control" name="EmployeeNumber" id="Employee Number">
</div>
<div class="form-group">
<label for="userName" class="form-control-label">User Name:</label>
<input type="text" class="form-control" name="UserName" id="userName">
</div>
<label for="department">Select Department</label>
<select class="form-control" id="department">
<option>Option1</option>
<option>Option2</option>
<option>Option3</option>
</select>
</form>
</div>
然后,使用jQuery根据名称分配值。根据您的数据情况,这可能会有所不同。应该是这样的:
$.ajax({
type: "GET",
url: '@Url.Action("GetUserDetails", "Admin")',
contentType: "application/json; charset=utf-8",
datatype: JSON,
data: ({ 'Value': id }),
success: function (result) {
$('#exampleModal form input[name=EmployeeNumber]').val(result.EmployeeNumber);
$('#exampleModal form input[name=UserName]').val(result.UserName);
},
error: function () {
alert(test);
}
});
答案 1 :(得分:0)
我建议你在你的模态内容中加载一个视图,例如:
function editUser(id) {
//alert(userId);
var $modal = $("#exampleModal");
$modal.find('.modal-body').load('@Url.Action("GetUserDetails", "Admin")/' + id, function() {
$modal.modal('show');
});
}
在控制器中,您可以获取模型数据并返回其视图,例如:
public ActionResult GetUserDetails(long id) {
UserDetailsViewModel model = GetModel(id); // Get the model somehow
return PartialView(model);
}
一个名为 GetUserDetails 的视图,例如:
@model UserDetailsViewModel
<form>
<div class="form-group">
@Html.LabelFor(model => model.EmployeeNumber)
@Html.EditorFor(model => model.EmployeeNumber, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserName)
@Html.EditorFor(model => model.UserName, new { @class = "form-control" })
</div>
<label for="department">Select Department</label>
<select class="form-control" id="department">
<option>Option1</option>
<option>Option2</option>
<option>Option3</option>
</select>
</form>
IMO是最优雅,最清晰的方式。