2013-02-05 2 views
3

이 면도날보기에는 이전 및 다음이라는 두 개의 단추가 있습니다.양식 내부에서 AJAX를 사용하여 메서드를 호출하는 방법은 무엇입니까?

이 같은
@using (Html.BeginForm("Index", "Quiz", FormMethod.Post)) 
{ 
<div style="border-bottom: 2px solid #c8c8c8; overflow: auto; width: 100%"> 
    <h2 class="float-left" style="padding-bottom:5px;">@Model.Quiz.Name</h2> 

    <div class="float-right"> 
     <input name="button" type="submit" value="done" /> 
    </div> 
</div> 

@for(...) { 
... 
} 

<div class="fixednavcontainer"> 
    <div id="questionnav" class="content-wrapper"> 
     <div id="questionnavstatus" class="float-left"> 
      <p> 
       Question <span id="currentPage">@ViewBag.CurrentPage</span> of <span id="totalPages">@ViewBag.TotalPages</span> 
      </p> 
     </div> 

     <div id="navbuttons" class="float-right"> 
      <img id="previous" src="~/Images/previous.png" /> 
      <img id="next" src="~/Images/next.png" /> 
     </div> 

     <div class="clear-fix" /> 
    </div> 
</div> 

뭔가 내가 아무것도 새로 고침하지 아약스 싶습니다

private void Save(@model model) 
{ 
    ... 
} 

버튼

호출해야합니다, 나는 사용자 유형을 저장하는 메소드를 호출해야하는 경우 사용자 언론이 버튼 페이지를로드하거나 다른 페이지를로드하고 같은 페이지를 저장하고 유지하십시오. jquery (내가 추측) 이후 action 메소드를 호출하는 것이 가능 할까?

+0

조치 방법에 어떤 값이 전달됩니까? 전체 모델? –

+0

@DaveA 예, 전체 모델 –

답변

3

또한 jQuery를 사용하고 이런 식으로 호출 할 수 있습니다 :

var myModel = @Html.Raw(Json.Encode(MyModel)); //MVC3 

$.ajax({ 
    type: "POST", 
    url: "/MyController/SomeAction/", 
    data: myModel , 
    cache: false, 
    dataType: "json", 
    success: function (response) { 
     // Do whatever you have to do... 
    } 
}); 

그리고이 같은 컨트롤러의 뭔가 :

public class MyController 
{ 
    [HttpPost] 
    public ActionResult SomeAction(MyModel myModel) 
    { 
     // Do whatever and return whatever 
      return true; 
    } 
} 
+0

전체 모델을 메서드에 전달하는 방법이 될까요? 제 말은,'FormCollection' 대신에 –

+0

예입니다. jQuery를 사용하여 양식을 직렬화하고 보낼 수 있습니다. 입력 이름이 모델 변수 이름과 같으면 모델로 선택됩니다. –

+0

예제를 업데이트하여 모델 전달 방법을 보여주었습니다. – c0deNinja

2

을 할 수 있습니다 단지

$.ajax({ 
    type: "POST", 
    url: "@Url.Action('yourAddress')", 
    data: parametr, 
    cache: false, 
    dataType: "json", 
    success: function (response) { 
     // Do whatever you have to do... 
    } 
}); 

와의

제어기

public class XController 
{ 
    [HttpPost] 
    public ActionResult yourAddress(YourModel) 
    { 
... 
    } 
} 
관련 문제