I want to call a controller when click the button in view.How can I do it in MVC?.
This is my first 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);
}
}
This is my view.
@{
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>
Then When I click the search button I want to check the input data.
Should I write new controller like this
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);
}
Or…
I don’t know how to call controller when click the button…
You want to wrap your user input fields and the submit button in a form. You can use an html helper that will also let you specify the controller action to call.
So modify your view:
Then in your controller you need to add the method to ‘catch’ this form. You’re already using a strongly typed view so capturing the data is easy