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">&times;</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」,但它不是可用,直到我發回。任何幫助都感激不盡。

謝謝

+0

您使用Ajax調用保存的價值。當您關閉彈出窗口要保存的值自身'VAR值= $(」 #selectedFeatureID')VAL(); $('#featureId')。val(value);' – SwapNeil

+0

不,它不是真的。 selectedFeatureID在模態窗體上,而featureId在我的主窗體上 –

回答

0

最後,我解決它通過添加子對象和新的局部視圖我控制器父對象上的取得動作。我將序列化對象傳遞給此操作並更新了我的部分視圖。

以下是我的代碼(可能是有同樣的問題的用戶有幫助)

渲染局部視圖,在我的主要觀點:

@Html.Partial("~/Views/Map/MapOpenPartial.cshtml", Model) 

Ajax調用在主視圖:

$.ajax({ 
    type: 'GET', 
    url: '@Url.Action("testMe","MyObject")', 
    cache: false, 
    data: $("#this_form").serialize(), 
    contentType: 'application/json; charset=utf-8', 
    success: function (partialViewResult) { 
     $("#trShowMap").html(partialViewResult); 
    } 
}); 

局部視圖:

@model Graafgebiedinformatie.Models.MYOBJECT 

@Html.HiddenFor(model => model.ProjectLocation.FeatureID, new { @id = "featureId" }) 
@Html.HiddenFor(model => model.ProjectLocation.RequestID) 

<td>@Html.Label("SpecifyLocation")</td> 
<td> 
    <a href="@Url.Action("ShowMap", "Map", new { requestId = Model.ProjectLocation.RequestID, featureId = Model.FEATURE_GUID_REF })" class="btn btn-default modal-link" id="startMap" aria-label="Left Align"> 
     <span class="glyphicon glyphicon-map-marker" aria-hidden="true"></span> @Graafgebiedinformatie.Globalization.Resources.SpecifyLocation 
    </a> 
</td> 

行動上主控制器:

public ActionResult testMe(MYOBJECT model) 
{ 
    return PartialView("~/Views/Map/MapOpenPartial.cshtml", model); 
}