2017-08-04 59 views
0

我嘗試了一些它的價值後,發送POST請求與模型數據該操作的性質發生變化:MVC AJAX郵寄更新模型數據

@{ 
    JsonSerializerSettings jss = new JsonSerializerSettings { 
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore }; 
} 

<div id="contents"> 

    <!--Lead Stage--> 
    @if (Model.LeadStagesNav != null) 
    { 
     for (int i = 0; i < Model.LeadStagesNav.Count; i++) 
     { 

      @Html.HiddenFor(a => a.LeadStagesNav[i].PermissionId) 

      <div class="form-group" style="margin-bottom:10px"> 
       @Html.Label("Lead Stage", new { @class = "col-md-2" }) 
       <div style="display:inline-block;position:relative"> 
        @Html.DropDownListFor(model => model.LeadStagesNav[i].Name, null, new { @class = "form-control", @style = "width:200px", onchange = "ChangeValue()" }) 
       </div> 

       @if (ViewData["LeadStagesNav[" + i + "].LeadStatus"] != null) 
       { 
         <!--Lead Status--> 
        @Html.Label("Lead Status", new { @style = "margin-left:15px;margin-right:15px" }) 
        <div style="display:inline-block;position:relative"> 
         @Html.DropDownListFor(model => model.LeadStagesNav[i].LeadStatus, null, new { @class = "form-control", @style = "width:200px", onchange = "ChangeValue()" }) 
        </div> 

        if (ViewData["LeadStagesNav[" + i + "].LeadSubStatus"] != null) 
        { 
         @Html.Label("Lead Sub Status", new { @style = "margin-left:15px;margin-right:15px" }) 
         <div style="display:inline-block;position:relative"> 
          <!--Lead Sub Status--> 
          @Html.DropDownListFor(model => model.LeadStagesNav[i].LeadSubStatus, null, new { @class = "form-control", @style = "width:200px" }) 
         </div> 
        } 
       } 

      </div> 

       <!--Delete Button--> 
      <div class="form-group"> 
       <div class="col-md-offset-2 col-md-10"> 
        <input type="submit" value="Delete Lead Stage" 
          onclick="document.getElementById('index').value = @i" 
          name="submit" class="btn btn-default" /> 
        <input type="hidden" id="index" name="index" /> 
       </div> 
      </div> 
      } 
     } 

</div> 

<script type="text/javascript"> 

window.ChangeValue = function() { 

    var model = @Html.Raw(JsonConvert.SerializeObject(Model, Formatting.Indented, jss)); 

    $.ajax({ 
     method: "POST", 
     url: "/CmsPermissions/Edit", 
     data: { permission: model }, 
     success: function (data) { 
      $("#contents").html(data); 
     }, 
     error: function (XMLHttpRequest, textStatus, errorThrown) { 
      alert(errorThrown); 
     } 
    }); 
}; 

的東西是的問題是,我得到的舊模型數據 張貼到行動而不是 新的數據後,下拉選擇的值已更改, 任何人有什麼想法?

+0

'VAR模型= @ Html.Raw(。你需要序列化你的表格 - 'data:$('form')。serialize(),'並且在'

' –

+0

中包裝你的表單控件但是如果這些'if'塊中的任何一個評估爲false,這會失敗,因爲默認情況下'DefaultModelBinder'需要收集索引器來進行明星t在零並且是連續的 –

+0

而且你每次在下拉列表中選擇一個選項時都會調用ajax,這是毫無意義的。你究竟想在這裏做什麼? –

回答

0

那是因爲你是路過老款車型的數據

var model = @Html.Raw(JsonConvert.SerializeObject(Model, Formatting.Indented, jss)); 

你需要序列化表單,並通過它的一個例子是

function SubmitForm() { 
    var data = $("#YourFormID").serialize(); 
    var url = "/YourURL/ACtion" 
    var form = $('#policyForm')[0] 
    var formdata = false; 
    if (window.FormData) { 
     formdata = new FormData(form); 
    } 
    return $.ajax({ 
     url: url, 
     type: 'POST', 
     dataType: 'json', 
     data: formdata ? formdata : data, 
     cache: false, 
     contentType: false, 
     enctype: 'multipart/form-data', 
     processData: false, 
     error: function() { 
      $('#imgLoadingForPortal').modal('hide'); 
      Lobibox.notify('error', { 
       size: 'mini', 
       rounded: true, 
       delay: false, 
       position: 'center top', //or 'center bottom' 
       msg: 'Something went wrong..please try again later', 
       sound: false, 
       delay: 5000, 
      }); 
     } 

    }) 

} 
+0

什麼是政策表? –

+0

它只工作一次,現在停止工作... –

+0

@YanivJacobJacob而不是政策表單將您的表單客戶端ID –