簡體   English   中英

單擊按鈕時如何調用控制器?

[英]how to Call controller when click the button?

單擊視圖中的按鈕時,我想調用控制器。如何在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);
}

要么...

單擊按鈕時,我不知道如何調用控制器...

您想將用戶輸入字段和“提交”按鈕包裝在表單中。 您可以使用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
}

您使用按鈕的方式不會發出請求。

您可以使用jQuery / javascript。 HTML:

使用Javascript:

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

}

或者,您可以使用@using(Html.BeginForm(..))包裝輸入,按鈕等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM