0
我想從使用引導模態的部分視圖更新對象模型屬性。我能夠更新主窗體上的隱藏值,並可以在發回後在模型屬性中看到它,但我想設置該屬性值而不回發。背後的原因是我在重新打開模態表單時使用了該值,但由於模型尚未回傳,因此無法使用。從引導3模式設置模型屬性無回發
隱藏控制模型特徵ID:
@Html.HiddenFor(model => model.ProjectLocation.FeatureID, new { @id = "featureId" })
的局部視圖錨標籤:
<a href="@Url.Action("ShowMap", "Map", new { requestId = Model.ID, featureId = Model.ProjectLocation.FeatureID })" class="btn btn-default modal-link" id="startMap" aria-label="Left Align">
<span class="glyphicon glyphicon-map-marker" aria-hidden="true"></span>SpecifyLocation
</a>
這裏是控制器:
public ActionResult ShowMap(string requestId, string featureId)
{
var model = new MapFeature(requestId);
model.FeatureID = featureId;
return PartialView("ShowMap", model);
}
這裏是我的局部視圖:
@using (Html.BeginForm("ShowMapPost", "Map", FormMethod.Post, new { @id = "frmMap" }))
{
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4>DrawProjectLocation</h4>
</div>
<div class="modal-body">
@Html.HiddenFor(model => model.FeatureID, new { id = "selectedFeatureID" })
@Html.HiddenFor(model => model.RequestID)
<div id="mainWindow" data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline', gutters:false" style="width:100%; height:100%;">
<div id="map" class="roundedCorners" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'"></div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success" id="submitButton">Save</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
}
<script>
$(document).ready(function() {
var frm = $("#frmMap");
frm.submit(function (e) {
e.preventDefault();
$.ajax({
url: "@Url.Action("ShowMapPost", "Map")",
type: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
FeatureID: frm.find('#selectedFeatureID').val()
}),
success: function (result) {
//or simply close our modal.
if (result) {
$('#modal-container').modal('hide');
}
else {
alert('LocationNotSelected');
}
},
error: function (result) {
alert('error');
}
});
});
});
</script>
這是JavaScript代碼,同時關閉我的模式形式,我使用:
$('#modal-container').on('hidden.bs.modal', function() {
var value = $('#selectedFeatureID').val();
$('#featureId').val(value);
$(this).removeData('bs.modal');
});
正如你可以看到,我想利用財產「Model.ProjectLocation.FeatureID」,但它不是可用,直到我發回。任何幫助都感激不盡。
謝謝
您使用Ajax調用保存的價值。當您關閉彈出窗口要保存的值自身'VAR值= $(」 #selectedFeatureID')VAL(); $('#featureId')。val(value);' – SwapNeil
不,它不是真的。 selectedFeatureID在模態窗體上,而featureId在我的主窗體上 –