Why httpPost model always null? - c#

I have get and post controller.
But by httpPost comtroller passing model parameter values are null.
Why my httpPost model parameter values always null ??
[HttpGet]
public ActionResult HireItem()
{
var HireItemListModel = new HireItemModel();
HireItemListModel = new HireItemModel()
{
first_name = Session["first_name"].ToString(),
middle_name = Session["middle_name"].ToString(),
last_name = Session["last_name"].ToString(),
ceremony_date = Session["ceremony_date"].ToString(),
};
var product = _productService.GetAllHireProducts();
if (product.Count != 0)
{
foreach (var proValue in product)
{
var productVarSeparateList = _productService.GetHireProductVariantsByProductIds(proValue.Id, false);
foreach (var HireProSep in productVarSeparateList)
{
var productVarSeparateModel = new HireItemModel.HireItemSeparatetModel()
{
pname = HireProSep.Name,
price =HireProSep.Price,
pId=HireProSep.Id,
};
HireItemListModel.HireItemSeparatetlist.Add(productVarSeparateModel);
}
var productVarSetList = _productService.GetHireProductVariantsByProductIds(proValue.Id, true);
foreach (var HireProset in productVarSetList)
{
var productVarListset = new HireItemModel.HireItemSetModel()
{
pname = HireProset.Name,
price = HireProset.Price,
pId = HireProset.Id,
};
HireItemListModel.HireItemSetList.Add(productVarListset);
}
}
}
return View(HireItemListModel);
}
This controller HireItemModel model parameter values are null. WHY??
[HttpPost,ActionName("HireItem")]
public ActionResult HireItem(string submitB, FormCollection formCollection, HireItemModel HireItemListModel)
{
var graduandList = _graduandService.GetGraduandBynameCeremony(HireItemListModel.ceremony_id, HireItemListModel.first_name, HireItemListModel.middle_name, HireItemListModel.last_name);
foreach (var graduand in graduandList)
{
graduand.height = HireItemListModel.height;
graduand.head_circumference = HireItemListModel.head_circumferenc;
_graduandService.Updategraduand(graduand);
}
this is my view.
#model HireItemModel
#using (Html.BeginForm())
{
<table >
<tr>
<td >
Ceremony :
</td>
<td>
Ceremony at #Model.ceremony_date
</td>
</tr>
<tr>
<td >
Name :
</td>
<td >
#Model.first_name #Model.middle_name #Model.last_name
</td>
</tr>
</table>
<div id="HItemType_1">
#Html.CheckBox("HItemType")
#*<input type="checkbox" name="test" value="test" id="HItemType" />*#
<label> Academic Dress Set</label>
</div>
<div id="HsetItem">
#Html.Partial("_LoadHireSetItem", #Model.HireItemSetList)
</div>
<div id="HseparateItem">
#Html.Partial("_LoadHireSeparateItem", #Model.HireItemSeparatetlist)
</div>
<table >
<tr>
<td colspan="2">
Please tell us your measurement:
</td>
</tr>
<tr>
<td >
Height (in cm):
</td>
<td>
#Html.EditorFor(model => model.height)
</td>
</tr>
<tr>
<td >
Head circumference (in cm):
</td>
<td >
#Html.EditorFor(model => model.head_circumferenc)
</td>
</tr>
</table>
<div>
<input class="productlistaddtocartbutton" type="submit" value="Add to cart" name="submitB" id="btnaddtocart"/>
</div>
}
thanks.

Make sure inside your view you have input fields for all values that you intend to use in your POST action. For example if you want to use the ceremony_id, first_name, middle_name, and last_name properties you need to include them:
#Html.HiddenFor(model => model.ceremony_id)
#Html.HiddenFor(model => model.first_name)
#Html.HiddenFor(model => model.middle_name)
#Html.HiddenFor(model => model.last_name)
You could use hidden fields if the user is not supposed to modify their values but you could also have used text fields depending on your requirements.

Related

How to add a counter with row numbering on all site in mvc

I want to add an automatic numbering of order items to all pages.
Below is an example that works but only numbers on one page. After going to the next page, it starts from the beginning.
public ActionResult ListaZlecen(int? IdStatusZlecenia, int strona = 1)
{
var ListaZlecenWszystkich = db.Zlecenia.ToList();
var userId = User.Identity.GetUserId();
//var ListaZlecen = db.Zlecenia.Where(p => p.UserId == userId).ToList();
//var ListaZlecen = db.Zlecenia.OrderBy(w => w.IdZlecenia).Where(p => p.UserId == userId).Skip((strona - 1) * WielkoscStrony).Take(WielkoscStrony).ToList();
var ListaZlecen = db.Zlecenia.OrderBy(w => w.IdZlecenia).Skip((strona - 1) * WielkoscStrony).Take(WielkoscStrony).ToList();
// var zleceniezalacznik = db.ZleceniaZalaczniki.ToList();
var viewodel = new ListaZlecenUzytkownikaViewModel()
{
StronaInfo = new StronaInfo
{
AktualnaStrona = strona,
PozycjeNaStrone = WielkoscStrony,
WszystkiePozycje = ListaZlecenWszystkich.Count()
},
ListaZlecenUzytkownika = ListaZlecen
};
return View(viewodel);
}
and View
#model AplikacjaHelpDesk.ViewModels.ListaZlecenUzytkownikaViewModel
#using AplikacjaHelpDesk.Infrastructure;
#{
ViewBag.Title = "Lista Zlecen Użytkownika";
Layout = "~/Views/Shared/_LayoutAdministracja.cshtml";
}
<div class="container-fluid">
<img src="~/Content/Images/Layout/Home.png" />
<a href="link">
#Html.MvcSiteMap().SiteMapPath()
</a>
<h2><span class="glyphicon glyphicon-user"></span>&nbsp<strong>Lista Zleceń </strong></h2>
<br /><br />
<div id="divLoading" class="panel panel-primary text-center text-primary" style="display:none;">
<h3><strong>Proszę czekać ładowane są posty!</strong></h3>
</div>
<div id="divLoadingForm" class="panel panel-primary text-center text-primary" style="display:none;">
<h3><strong>Proszę czekać ładuję formularz</strong></h3>
</div>
#*#if (ViewBag.Informacja != null)
{
<div class="alert alert-warning"><h4><strong>#TempData["Dodano-Post"]</strong></h4></div>
}*#
<table class="table table-responsive table-striped transactions" style="text-combine-upright:all;">
<tr style="text-transform: uppercase; text-combine-upright:all;">
<th>
<label>Pozycja Nr.</label>
</th>
<th>
<label>Nr Zlecenia</label>
</th>
<th>
<label>Data Przyjęcia Zlecenia</label>
</th>
<th>
<label>Data Planowanego Zakończenia Zlecenia</label>
</th>
<th>
<label>Data zakończenia zlecenia</label>
</th>
<th style="width: 160px;"></th>
<th style="width: 160px;"></th>
</tr>
#{ var i = 0;}
#foreach (var item in Model.ListaZlecenUzytkownika)
{
<tr class="panel panel-primary">
#*
<h5><strong>Zlecenie nr: #Html.DisplayFor(modeItem => item.IdZlecenia)</strong></h5>
<td>
#{i++;}
#i
</td>
<td>
<h5><strong>Zlecenie nr: #Html.DisplayFor(modeItem => item.IdZlecenia)</strong></h5>
</td>
<td>
#Html.DisplayFor(modelItem => item.DataPrzyjeciaZlecenia)
</td>
<td>
#Html.DisplayFor(modelItem => item.DataPlanowaniaZakonczenia)
</td>
<td>
#Html.DisplayFor(modelItem => item.DataZakonczenia)
</td>
<td>
#Ajax.ActionLink("Pokaż Posty Zlecenia", "_ListaPostow", new { idZlecenia = #item.IdZlecenia }, new AjaxOptions()
{
HttpMethod = "GET",
LoadingElementId = "divLoading",
UpdateTargetId = "divPozycje",
InsertionMode = InsertionMode.Replace
}, new { #class = "btn btn-primary" })
</td>
<td>
#Ajax.ActionLink("Dodaj Odpowiedz", "_DodajPost", new { idZlecenia = #item.IdZlecenia }, new AjaxOptions()
{
HttpMethod = "GET",
LoadingElementId = "divLoadingForm",
UpdateTargetId = "divDodajPozycje",
InsertionMode = InsertionMode.Replace
}, new { #class = "btn btn-primary" })
</td>
</tr>
<tr style="background: #23527c; color:white;">
<td>
<label>Opis załącznika</label>
</td>
<td style="width: 120px;">
<label>Załącznik</label>
</td>
</tr>
<tr class="panel panel-group">
<td>
#Html.Raw(item.ZleceniaZalaczniki.Opis)
</td>
<td>
<span class="btn btn-primary">
#Html.ActionLink("Pobierz", "Download", "Zlecenia", new { nazwaPliku = #item.ZleceniaZalaczniki.NazwaPliku }, null)
<span class="glyphicon glyphicon-download" aria-hidden="true"></span>
</span>
</td>
</tr>
<tr id="divDodajPozycje"></tr>
}
</table>
<br />
<div class="btn-group pull-right">
#Html.LinkStrony(Model.StronaInfo, x => Url.Action("ListaZlecen", new { strona = x }))
</div>
<br />
<hr />
<div id="divPozycje">
</div>
</div>
I tried this way but it creates numbering only on one page. Going to the next numbering is created from the beginning.
#{ var i = 0;}
<td>
#{i++;}
#i
</td>
I have 3 orders on each page and I would like all orders to be automatically numbered, for example:
First page 1,2,3
Second page 4,5,6
I am asking for help in creating the numbering for the entire collection of orders
Instead of starting at i=0 you can start at your current page,
I don't know which language it is but I hope i got it right.
// Instead of this: #{ var i = 0;} - on your view
//Use this:
#{ var i = (Model.AktualnaStrona -1 ) * Model.PozycjeNaStrone ;}
//It should be equal to the calculation you use on the API:
(strona - 1) * WielkoscStrony
//(from this line: )
var ListaZlecen = db.Zlecenia.OrderBy(w => w.IdZlecenia).
Skip((strona - 1) * WielkoscStrony). //--> THIS
Take(WielkoscStrony).ToList();

ASP.NET MVC 4 - razor empty post in for loop

I have the following code in razor:
#for (int i = 0; i < Model.Count; i++)
{
using (Html.BeginForm("Bonnen", "Bon"))
{
<tr>
<td> #Html.TextBoxFor(model => Model[i].Id)</td>
<td>#Html.DisplayFor(model => Model[i].Date)</td>
<td>#Html.DisplayFor(model => Model[i].Description)</td>
<td><button type="submit" value="Details" class="btn btn-info">Details</button></td>
</tr>
}
}
When i post the data to the controller it becomes empty.
I've already seen a question where it said you can't use a foreach loop here.
On the website itself it does show all the data, but it won't give it to the controller.
Controller:
[HttpPost]
public ActionResult Bonnen(Bon bon)
{
return RedirectToAction("Details", "Bon", bon);
}
Model:
public int Id { get; set; }
public string Description { get; set; }
public DateTime Date { get; set; }
Your code needs to be inside the form and button type "submit" wont work. You need to use input type "submit". See code below:
#using (Html.BeginForm("Bonnen", "Bon"))
{
#for (int i = 0; i < Model.Count; i++)
{
<tr>
<td> #Html.TextBoxFor(model => Model[i].Id)</td>
<td>#Html.DisplayFor(model => Model[i].Date)</td>
<td>#Html.DisplayFor(model => Model[i].Description)</td>
<td><input type="submit" value="Details" class="btn btn-info"/></td>
</tr>
}
}
#using (Html.BeginForm("Bonnen", "Bon",FormMethod.Post))
{
#foreach (var item in Model)
{
<tr>
<td> #Html.TextBoxFor(model => item.Id)</td>
<td>#Html.DisplayFor(model => item.Date)</td>
<td>#Html.DisplayFor(model => item.Description)</td>
</tr>
}
<input type="submit" value="Details" class="btn btn-info"/>
}
[HttpPost]
public ActionResult Bonnen(int[] Id ,DateTime[] Date,string[] Description)
{
return RedirectToAction("Details", "Bon", bon);
}
Try the following code
#for (int i = 0; i < Model.Count; i++)
{
using (Html.BeginForm("Bonnen", "Bon"))
{
<tr>
<td> #Html.TextBox("Id", Model[i].Id)</td>
<td>#Html.Display("Date", Model[i].Date)</td>
<td>#Html.Display("Description", Model[i].Description)</td>
<td><button type="submit" value="Details" class="btn btn-info">Details</button></td>
</tr>
}
}
If you want the values of Date and Description too in controller then change them from Display to Editor.
managed to solve it with a foreach loop.
#using (Html.BeginForm())
{
<h2>Bon</h2>
<input class="form-control" id="myInput" type="text" placeholder="Zoek..." />
<br />
<table style="width: 100%">
<thead>
<tr>
<th>Id</th>
<th>Datum</th>
<th>Boodschappen</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Id)
</td>
<td>
#Html.DisplayFor(modelItem => item.Date)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#using (Html.BeginForm("Bon", "Bon", FormMethod.Post))
{
#Html.AntiForgeryToken()
#Html.ActionLink("Details", "Details", "Bon", new { id = item.Id }, null)
}
</td>
</tr>
}
</tbody>
</table>
}

Why are the parameters of my action null?

I'm trying to make a page using ASP MVC 5 with C# in which I have some controls to type search parameters and a table where the results are shown.
The thing is that the user should be allowed to edit the text of two fields in the table and save the changes.
To accomplish this, my view receives a model with two objects as properties, one for the search parameters and one for the result list, like this:
public class SEARCH_PAGE
{
public List<VIEW_DATA_APR> table{ get; set; }
public SEARCH parameters{ get; set; }
}
Then my Controller is this:
public class CargaAPRController : Controller
{
private datasource db = new datasource();
// GET: CargaAPR
public ActionResult Index()
{
try
{
List<SelectListItem> items = new SelectList(db.COMPANY, "IDCOMPANY", "COMPANYNAME").ToList();
items.Insert(0, (new SelectListItem { Text = "ALL", Value = "0" }));
ViewData["IDCOMPANY"] = items;
var letters = (from c in db.LETTERS
select new VIEW_DATA_APR
{
company = c.COMPANY.COMPANYNAME,
idLetter = c.IDLETTER,
nic = "not found",
client = "not found",
energy = 0,
money = 0,
period = "",
letterCode = c.LETTERCODE
});
SEARCH_PAGE sp = new SEARCH_PAGE();
sp.table= letters.ToList();
sp.parameters= new SEARCH();
return View(sp);
}
catch (Exception ex)
{
return RedirectToAction("Error", new RouteValueDictionary(new { controller = "Users", action = "Error", mensaje = "Error: " + ex.Message }));
}
}
[HttpPost]
public ActionResult Index(SEARCH_PAGE model)
{
try
{
List<SelectListItem> items = new SelectList(db.COMPANY, "IDCOMPANY", "COMPANYNAME").ToList();
items.Insert(0, (new SelectListItem { Text = "ALL", Value = "0" }));
ViewData["IDCOMPANY"] = items;
decimal company = Decimal.Parse(model.parameters.company);
var letters= (from c in db.LETTERS
where (company== 0 ? c.IDCOMPANY: company) == c.IDCOMPANY
select new VIEW_DATA_APR
{
company= c.COMPANY.COMPANYNAME,
idLetter= c.IDLETTER,
nic = "not found",
client = "not found",
energy = 0,
money = 0,
period = "",
letterCode = c.LETTERCODE
});
SEARCH_PAGE sp = new SEARCH_PAGE();
sp.table= letters.ToList();
sp.parameters = model.parameters;
return View(sp);
}
catch (Exception ex)
{
return RedirectToAction("Error", new RouteValueDictionary(new { controller = "Users", action = "Error", mensaje = "Error: " + ex.Message }));
}
}
[HttpPost]
public ActionResult Save(SEARCH_PAGE model_search_page )
{
return View();
}
}
And my View is:
#using datasource.Models
#model SEARCH_PAGE
#{
ViewBag.Title = "Load APR";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#Styles.Render("~/Content/energetica.css")
<meta name="viewport" content="width=device-width" />
<title>Letters</title>
<link href="#Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<div class="container-fluid">
<div class="col-md-1">
</div>
<div class="col-md-10">
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">
<div class="row">
<div class="col-md-1">
<a href="#Url.Action("Index", "LoadFiles")" class="elements">
<img class="img img-responsive" style="width:25px; height:26px;padding:1px" src="~/img/BackIcon.png">
</a>
</div>
<div class="col-md-11 text-left" style="padding:1px;">
LOAD APR
</div>
</div>
</div>
</div>
#using (Html.BeginForm("Index","LoadAPR", FormMethod.Post, null))
{
<table style="width:100%">
<tr>
<td style="width:10%">
<b>COMPANY: </b>
</td>
<td style="width:20%">
#Html.DropDownListFor(m => m.parameters.company, (IEnumerable<SelectListItem>)ViewData["IDCOMPANY"], new { htmlAttributes = new { #class = "form-control" } })
</td>
<td style="width:10%">
<b>PERIOD: </b>
</td>
<td style="width:20%">
#Html.EditorFor(m => m.parameters.period)
</td>
<td style="width:20%; text-align:right">
<input type="submit" name="SEARCH" value="SEARCH" />
</td>
</tr>
<tr>
<td style="width:10%">
<b>CLIENT: </b>
</td>
<td style="width:20%">
#Html.EditorFor(m => m.parameters.client)
</td>
<td style="width:10%">
<b>NIC: </b>
</td>
<td style="width:20%">
#Html.EditorFor(m => m.parameters.nic)
</td>
</tr>
</table>
}
<br />
#using (Html.BeginForm("Save", "LoadAPR", FormMethod.Post, null))
{
<div style="overflow-y: scroll; max-height: 300px">
<table style="width:100%">
<tr>
<th>
#Html.Label("LBDIS", "Company")
</th>
<th>
#Html.Label("LBNLETTER", "Letter Code")
</th>
<th>
#Html.Label("LBNIC", "NIC")
</th>
<th>
#Html.Label("LBCLIENT", "Client")
</th>
<th>
#Html.Label("LBENERGY", "Energy")
</th>
<th>
#Html.Label("LBMONEY", "Money")
</th>
</tr>
#foreach (var item in Model.table)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.company)
</td>
<td>
#Html.DisplayFor(modelItem => item.letterCode)
</td>
<td>
#Html.DisplayFor(modelItem => item.nic)
</td>
<td>
#Html.DisplayFor(modelItem => item.client)
</td>
<td>
#Html.EditorFor(modelItem => item.energy)
</td>
<td>
#Html.EditorFor(modelItem => item.money)
</td>
</tr>
}
</table>
</div>
<br />
<div style="width:100%; text-align:right;">
<input class="btn-addUser" type="submit" value="Save" />
</div>
}
</div>
So when I run it and click on my first button, the POST Index parameter receives just the parameters part of the object, the table part is null
And when I click on my second button, the POST Save parameter comes null in both properties.
I have tried so many different ways of seding the Model as parameter but nothing is working. Could you tell me what's wrong? Am I using he BeginForm in the correct way?
Thank you in advance.

c# MVC Dropdownlist post selected value

Let me start by saying this: I have looked at multiple answers, and I can't just seem to wrap my head around it - it may be that I'm just brainfarted. But here goes my question:
My question is specifically regarding the dropdownlist: With the hardcoded values: today, month, year.
Whenever I select something in the dropdownlist, I want to post/submit the value, for example "today" to the controller. How do I do this?
Thanks for your time.
#using (Html.BeginForm("DisplayDetails", "Client"))
{
#*Start of searchBox*#
<div id="searchBox">
Enter client e-mail: <input id="txtUser" type="text" name="clientID" /> <input id="btnFind" type="submit" />
</div>
#*<--- End of searchBox --->*#
if (Model != null)
{
#*Start of infobox and wrapper*#
<div id="infoBoxWrapper">
<div class="infoBox">
<fieldset>
<legend style="padding: 0.2em 0.5em; border: 1px solid black; font-size: large;"> Client info </legend>
<table>
<tr>
<td> <b> First name: </b></td>
<td>
#Html.DisplayFor(m => m.FName)
</td>
</tr>
<tr>
<td> <b> Last name: </b></td>
<td> #Html.DisplayFor(m => m.LName) </td>
</tr>
<tr>
<td> <b> Phone: </b></td>
<td> #Html.DisplayFor(m => m.Phone)</td>
</tr>
<tr>
<td> <b> E-mail: </b></td>
<td> #Html.DisplayFor(m => m.UserID) </td>
</tr>
</table>
</fieldset>
</div>
#*<--- End of infobox --->*#
#*Start of recordsBox*#
<div id="recordsBox">
<fieldset>
<legend style="padding: 0.2em 0.5em; border: 1px solid black; font-size: large;"> Records </legend>
<table>
<tr>
<td> <b>Time</b></td>
<td> <b>Systolic</b> </td>
<td> <b>Diastolic</b> </td>
<td> <b>Pulse</b> </td>
</tr>
#for (int i = 0; i < Model.Records.Count; i++)
{
string style = null;
if (Model.Records[i].Score == 1) { style = "background-color:green"; }
else if (Model.Records[i].Score == 2) { style = "background-color:blue"; }
else if (Model.Records[i].Score == 3) { style = "background-color:yellow"; }
else if (Model.Records[i].Score == 4) { style = "background-color:orange"; }
else if (Model.Records[i].Score == 5) { style = "background-color:red"; }
<tr style="#style">
<td>
#Model.Records[i].Time
</td>
<td>
#Html.DisplayFor(m => m.Records[i].Systolic)
</td>
<td>
#Html.DisplayFor(m => m.Records[i].Diastolic)
</td>
<td>
#Html.DisplayFor(m => m.Records[i].Pulse)
</td>
</tr>
}
</table>
</fieldset>
</div>
</div>
#*<--- End of infobox wrapper --->*#
#*Start of chartbox*#
<div id="chartWrapper">
<div id="comboBox">
<select id="dpChoice">
<option value="today">Today</option>
<option value="month">This month</option>
<option value="year">This year</option>
</select>
</div>
<div id="chartbox">
#Model.Chart
</div>
</div>
#*<--- End of chartbox --->*#
}
else
{
<label> Search for a client...</label>
}
}
Your select does not have a name attribute so it is not sent in the form data. Change it to (say)
<select name="choice">
...
</select>
and depending if the first option is selected it will post back choice: today which you can access by adding the parameter string choice to your POST method. However I would strongly recommend you use a view model including
public class MyViewModel
{
public int ClientEmail{ get; set; }
public string Choice { get; set; }
public IEnumerable<SelectListItem> ChoiceList { get; set; }
....
}
and populate the collection in the controller
model.ChoiceList = new List<SelectListItem>()
{
new SelectListItem(){ Value = "today", Text = "Today" },
new SelectListItem(){ Value = "month", Text = "This month" },
}
and then in the view
#Html.TextBoxFor(m => m.ClientEmail)
....
#Html.DropDownListFor(m => m.Choice, Model.ChoiceList)
and then post back the view model to the controller

how to Call controller when click the button?

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:
...
#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")" />
})
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
[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
}
The way you have your button, it won't make the request.
You can use jQuery/javascript.
HTML:
Javascript:
function callController()
{
$.get("YourController/Action",data, callBackMethod);
}
Or you can wrap your input, buttons etc... using #using(Html.BeginForm(..))

Categories