gpt4 book ai didi

c# - 单击按钮时如何调用 Controller ?

转载 作者:行者123 更新时间:2023-11-30 21:03:30 25 4
gpt4 key购买 nike

我想在 View 中单击按钮时调用 Controller 。我如何在 MVC 中执行此操作?

这是我的第一个 Controller 。

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>

然后,当我单击搜索按钮时,我想检查输入数据。我应该像这样写新的 Controller 吗

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

或者...

我不知道如何在点击按钮时调用 Controller ...

最佳答案

您想将用户输入字段和提交按钮包装在一个表单中。您可以使用 html 帮助程序,它还可以让您指定要调用的 Controller 操作。所以修改你的观点:

...
@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")" />

})

然后在您的 Controller 中,您需要添加方法来“捕获”此表单。您已经在使用强类型 View ,因此捕获数据很容易

[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
}

关于c# - 单击按钮时如何调用 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12810687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com