2012-10-10 50 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); 
    }    
} 

這是我的看法。

@{ 
    Layout = "~/Views/Shared/_ColumnsThree.cshtml"; 
} 

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

<table> 
    <tr> 
     <td> 
      @Html.LabelFor(model => model.ceremony_id) 
     </td> 
     <td> 
      @Html.DropDownListFor(model => model.ceremony_id, Model.AvailableCeremony) 
     </td> 
    </tr> 
    <tr> 
     <td> 
      @Html.LabelFor(model => model.first_name): 
     </td> 
     <td> 
      @Html.EditorFor(model => model.first_name)  
     </td> 
    </tr> 
    <tr> 
     <td> 
      @Html.LabelFor(model => model.middle_name): 
     </td> 
     <td> 
      @Html.EditorFor(model => model.middle_name) 
     </td> 
    </tr> 
    <tr> 
     <td> 
      @Html.LabelFor(model => model.last_name): 
     </td> 
     <td > 
      @Html.EditorFor(model => model.last_name) 
     </td> 
    </tr> 
    <tr> 
     <td> 
      @Html.LabelFor(model => model.student_id): 
     </td> 
     <td> 
      @Html.EditorFor(model => model.student_id) 
     </td> 
    </tr>  
    <tr> 
     <td colspan="2" class="buttons"> 
      <input type="submit" id="btnsearchgraduand" name="btnsearch" 
       class="searchbutton" value="@T("Search")" /> 
     </td> 
    </tr> 
</table> 

然後當我點擊搜索按鈕時,我想檢查輸入數據。 我應該寫新的控制器這樣

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

或者......

我不知道怎麼稱呼控制器單擊按鈕時...

回答

3

你想換你的用戶輸入字段和表單中的提交按鈕。你可以使用一個html helper,它也允許你指定要調用的控制器動作。 所以修改你的觀點:

... 
@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

你有你的按鍵的方式,它不會提出請求。

您可以使用jQuery/javascript。 HTML:

的Javascript:

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

} 

或者你可以用你的輸入,按鈕等...使用@using(Html.BeginForm(..))

相關問題