2017-08-04 2 views
0

更新されたモデルデータを送信する:MVC AJAXポストは、私はそれのプロパティの一部の値が変更された後、モデルデータをアクションにPOSTリクエストを送信しようとしている

@{ 
    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); 
     } 
    }); 
}; 

事問題は私が古いモデルのデータを取得することです 代わりに 新しいデータのドロップダウン選択値が変更された後に、アクションに投稿 誰もが考えている?あなたがデータ

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

として古いモデルを渡している、あなたのフォームをシリアル化し、それを例に合格する必要があるためである

+0

ある 'VARモデル= @ Html.Raw(。 '' data:$( 'form')。serialize()、 '

' –

+0

でフォームコントロールをラップする必要があります。 'defaultModelBinder'はデフォルトでコレクションインデクサーにスターを付ける必要があるため、これは失敗しますtをゼロにして連続する –

+0

そして、ドロップダウンリストの1つでオプションを選択するたびにajax関数を呼び出すことは意味がありません。実際にここで何をしようとしていますか? –

答えて

0

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

policyformとは何ですか? –

+0

これは一度だけ動作し、現在は動作を停止しました。 –

+0

ポリシーフォームの代わりに@YanivJacobJacobフォームクライアントIDを入力してください –

関連する問題