2017-10-11 1 views
0

잠시 동안 누적되었다는 문제점이 있습니다. 나는이 물건에 대해 약간 새로운 것이므로 조금만 참아주십시오. 나는 해결하는 것이 아주 간단하다고 생각하지만 지금은 서클을 운영 중이다. 나의 의도는 방법이 완료되었음을 알리는 간단한 경고를하는 것이다. 아래에서 볼 수 있듯이 간단한 문자열 및 if 문으로 내 첫 시도가 이루어졌습니다. 더 이상 페이지를 새로 고침하고 싶지 않지만 지금은 메서드가 끝나는 경고 스크립트가 시작되는 문제가 있습니다.
그래서 나는 컨트롤러 있습니다컨트롤러의 메소드 성공 후 Ajax 경고

 [HttpPost] 
    public ActionResult Executor(LMCMainViewModel model) 
    { 
     var curlsExecutor = _curls; 
     var applicationPurgeCurl = curlsExecutor.GetApplicationCurl(model); 
     var temporary = model.SuccessExecutionStatus = curlsExecutor.CurlCaller(model.ApplicationList.ApplicationCurl); 
     var tempListOfApplications = PopulateModel(model); 
     model.ApplicationList.ListOfApplications = tempListOfApplications.ApplicationList.ListOfApplications; 
     model.SuccessExecutionStatus = temporary; 
     return View("ListOfApplications", model); 
    } 

을 그리고 난 내 보기이 : 나는 아약스 스크립트의 많은 변화를 구현하기 위해 노력하지만 난 그래서 내가 할 수있는 것을 꼬여있어보다

   @model LMC.Models.LMCMainViewModel 
      @{ 
       ViewData["Title"] = "Liberation"; 
      } 
      @using (Html.BeginForm("HeaderString", "LMC")) 
      { 
      } 
      @using (Html.BeginForm("Executor", "LMC", FormMethod.Post)) 
      { 

       <div class="col-sm-2" asp-action="ListOfApplications"> 
        @Html.DropDownListFor(x => x.ApplicationList.ChosenApplication, Model.ApplicationList.ApplicationListItem, new { @id = "DropdownID" }) 
       </div> 

       <div class="col-sm-2 col-sm-push-5"> 
        @Html.HiddenFor(x => x.ApplicationList.ApplicationListItem) 
        <input class="btn ctn-success" id="submit" type="submit" value="Submit" /> 
        <button id="submitButtonAjax" type="button" class="btn btn-success">Ajax button</button> 

        <div class="col-sm-12"> 
         @Html.Label(null, (Model.SuccessExecutionStatus ? "Success" : " "), 
         new { Style = Model.SuccessExecutionStatus ? "color: green;" : "color: red;" }) 


        </div> 
       </div> 
      } 

심지어 가장 좋은 글을 게시하지 않습니다 ... 내가 아는 한 가지는 : @using (Html.BeginForm("Executor", "LMC", FormMethod.Post))을 제거하고 Ajax에 배치하려고하면 간단히 작동하지 않습니다. 제 의도는 다음과 같이 작동하는 것입니다 :

<script type="text/javascript"> 
    $("#submitButtonAjax").on("click", function() { 
     $.ajax({ 
      type: 'POST', 
      url: "/LMC/Executor", 
      success: function() { 
       alert("Went well"); 
      } 
    }); 
</script> 

컨트롤러 메소드를 Json으로 리턴하려고 시도했지만 작동하지 않았습니다. 비슷한 조언을 많이 읽을 수있는 조언을 해주시면 감사하겠습니다. 비슷한 질문이 많이 있다는 것을 알고 있습니다 만, 제 질문에 한 걸음 뒤로 물러서서 제 코드로 작업하는 것을 구현할 수 없습니다. 다른 사람들과의 비교) 아니면 어쩌면 그렇게 쉽고 나는 뭔가를 놓치고 해결책을 게시 할 수 있습니다. 어쨌든, 어떤 도움을 많이 주셔서 감사합니다.

+0

모양을 사용할 수 있습니다. 마술처럼 페이지를 비동기로 만들지는 않습니다. 나는 이것이 더 어떻게 작동하는지에 대해 읽어 볼 것을 제안한다. – Liam

+0

[Jquery AJAX를 사용하여 HTML 폼 제출하기] (https://stackoverflow.com/questions/16323360/submitting-html-form-using-jquery-ajax) – Liam

답변

0

AJAX 호출은

$(document).ready(function() { 
     $("#submitButtonAjax").on("click", function() { 
      var postData = { 
       FirstPropertyOfTheModel: ValueForTheFirstProperty, 
       SecondPropertyOfTheModel: ValueForTheSecondProperty, 
      }; 

     $.ajax({ 
      type: 'POST', 
      url: "/LMC/Executor", 
      data:postData, 
      success: function() { 
       alert("Went well"); 
      }, 
      error: function() { 
       alert("Opssss not working"); 
      } 
     }); 
    }); 
    }); 

데이터 컨트롤러 ActionResult 방법의 모델에 대한 값 노호 같을 것이다. FirstPropertyOfTheModel과 SecondPropertyOfTheModel은 속성의 이름으로 대체되고 해당 값을 존중 적으로 할당합니다. URL에서 당신은 내가 $의 .ajax` 당신이 그것을하지 생각하지`생각하지 않는다

'@Url.Action("Executor", "LMC")' 

그렇게 아약스가

$(document).ready(function() { 
      $("#submitButtonAjax").on("click", function() { 
       var postData = { 
        FirstPropertyOfTheModel: ValueForTheFirstProperty, 
        SecondPropertyOfTheModel: ValueForTheSecondProperty, 
       }; 

      $.ajax({ 
       type: 'POST', 
       url: '@Url.Action("Executor", "LMC")', 
       data:postData, 
       success: function() { 
        alert("Went well"); 
       }, 
       error: function() { 
        alert("Opssss not working"); 
       } 
      }); 
     }); 
     }); 
관련 문제