2012-10-10 5 views
2

보기에서 버튼을 클릭 할 때 컨트롤러를 호출하고 싶습니다. MVC에서 어떻게 할 수 있습니까?.버튼을 클릭 할 때 컨트롤러를 호출하는 방법은 무엇입니까?

이것은 내 첫 번째 컨트롤러입니다.

public ActionResult DetailForm() 
{ 
    graduandModel model = new graduandModel(); 

    var ceremonyList = _ceremonyService.GetAllCeremonyByDate(DateTime.Now); 

    if (ceremonyList.Count == 0) 
    { 
     return Content("No ceremony can be loaded"); 
    } 
    else 
    { 
     foreach (var c in ceremonyList) 
     { 
      model.AvailableCeremony.Add(new SelectListItem() { 
       Text = "Ceremony at " + c.ceremony_date.ToString(), 
       Value = c.ceremony_id.ToString() }); 
     } 
     return View(model); 
    }    
} 

이것은 내 견해입니다.

그런 다음 검색 버튼을 클릭하면 입력 데이터를 확인하고 싶습니다. 내가 작성해야 ...

답변

3

당신은 사용자 입력 필드를 감싸고있는 형태 버튼을 제출할 사용 . 호출 할 컨트롤러 조치를 지정할 수있는 html 도우미를 사용할 수 있습니다. 그래서보기를 수정

... 
@model graduandModel 
@using Nop.Web.Models.Hire; 
@using Nop.Web.Framework; 
@using Telerik.Web.Mvc.UI; 
@using System.Linq; 

@using(Html.BeginForm("DetailForm", "ControllerName", FormMethod.Post, new {enctype="multipart/form-data"}) 
{ 
<table > 

<tr> 
    <td > 
     @Html.LabelFor(model => model.ceremony_id) 
    </td> 
... 

//code omitted for brevity 

... 

<input type="submit" id="btnsearchgraduand" name="btnsearch" class="searchbutton" value="@T("Search")" /> 

}) 

그런 다음 컨트롤러에서 당신은 '캐치'이 양식 방법을 추가해야합니다. 이미 강력한 형식의보기를 사용하여 데이터를 쉽게 캡처 할 수 있습니다.

[HttpPost] 
public ActionResult DetailForm (graduandModel model) 
{ 
//do what you need to do with the data here 
//the model passed into this method's parameter should 
//contain all the data from the editor templates in the view 
} 
1

I 버튼을 클릭 할 때 컨트롤러를 호출하는 방법을 모르는

public ActionResult CheckDegreeDetails() 
{ 
    graduandModel model = new graduandModel(); 

    var degreeList = _graduandService.GetGraduandByStudent(
     model.ceremony_id, model.first_name, 
     model.middle_name, model.last_name); 
    return View(model); 
} 

또는 ... 같은 새로운 컨트롤러 당신이 당신의 버튼이 방법, 요청하지 않습니다.

jQuery/javascript를 사용할 수 있습니다. HTML :

자바 스크립트 :

function callController() 
{ 
$.get("YourController/Action",data, callBackMethod); 

} 

또는 당신이 당신의 입력을 포장 수, 버튼 등 ... @using(Html.BeginForm(..))

관련 문제