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
Related
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>
}
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.
I'm trying to edit data though angularjs and I've got all the data in view - but the only problem is that the dropdownlist does not select the value that is passed to it and I don't know why.
JS Code:
function getCustomer() {
var promiseGetStudent = SPACRUDService.getCustomer(ShareData.value);
promiseGetStudent.then(function (pl) {
pl.data.MirageDate = $filter('date')(pl.data.MirageDate, 'yyyy-MM-dd');
$scope.Cust = pl.data;
},
function (errorPl) {
$scope.error = 'failure loading Student', errorPl;
});
}
HTML Code:
<table class="table">
<td style="width:200px; text-align:center; vertical-align:central">إسم العميل</td>
<td>
<table class="table" style="margin-bottom: -3px;">
<tr>
<td style="padding:0; vertical-align:central; width:50%;">
<div class="input-group">
<span class="input-group-addon ion ion-person"></span>
<input type="text" class="form-control" placeholder="إسم العميل" ng-model="Cust.CustomerName">
</div>
</td>
<td style="width:125px; vertical-align:central" class="text-center">المدينة</td>
<td style="padding:0; vertical-align:central">
<input type="text" ng-model="Cust.CityID" name="cid" id="cid" />
#Html.DropDownList("CityID", null, "إختر المدينة", htmlAttributes: new { #class = "form-control", #style = "width=100%", #ng_model = "Cust.CityID" })
</td>
</tr>
</table>
</td>
</tr>
</table>
I get the value of "CityID" but the dropdownlist does not select it from the items, and when I look into html of the dropdownlist I found:
<option value="? number:2 ?"></option>
So please what is the problem here and how can I make it work?
I need to send the coils selected on "AuctionPage" to the another table on "MyBids" Page. I think it would be best to grab the coilID and pass it that way but not sure how? Any help would be appreciated.
AuctionPage-->
#model NucorPrototypes_V1.ViewModel.AuctionPage
#{
ViewBag.Title = "Index";
}
<title>Auction </title>
<div id="header" style="background-color: #008751">
<h1 style="margin-bottom: 0; color: #FFFFFF">Secondary Coil Auction </h1>
</div>
<div id="content">
<h2>Index</h2>
<table id="myTable" border="3" style="background-color: #B0B0B0" cellpadding="10" class="table">
<tr>
<th>Select</th>
<th>Serial Number</th>
<th>Minimum Bid</th>
<th>Current High Bid</th>
<th>Grade</th>
<th>Heat Number</th>
<th>Gauge</th>
<th>Width</th>
<th>Weight</th>
<th>Defect 1</th>
<th>Defect 2</th>
<th>Defect 3</th>
</tr>
#foreach (var item1 in Model.Coils)
{
<tr>
<td>
<input type="checkbox" name="selection" id="#item1.Coil_ID" align="center">
</td>
<td>
#Html.DisplayFor(modelItem => item1.Coil_ID)
</td>
<td>
#foreach (var item2 in Model.Auctions)
{
#Html.DisplayFor(modelItem => item2.Auc_MinBid)
}
</td>
<td>
#foreach (var item3 in Model.Bids)
{
#Html.DisplayFor(modelItem => item3.Bid_Amt)
//string sqlq = "select max(Bid_Amt) from Bid inner join AuctionItem ON Bid.Bid_ID = AuctionItem.Bid_ID where Coil_ID =" + item1.Coil_ID +"' '";
}
</td>
<td>
#Html.DisplayFor(modelItem => item1.Coil_Grade)
</td>
<td>
<a data-toggle="modal" data-target="#myModal">#Html.DisplayFor(modelItem => item1.Coil_HeatNo)</a>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">#Html.DisplayFor(modelItem => item1.Coil_HeatNo)</h4>
</div>
<div class="modal-body">
CHemistries
</div>
</div>
</div>
</div>
</td>
<td>
#Html.DisplayFor(modelItem => item1.Coil_Gauge)
</td>
<td>
#Html.DisplayFor(modelItem => item1.Coil_Width)
</td>
<td>
#Html.DisplayFor(modelItem => item1.Coil_Weight)
</td>
<td>
#Html.DisplayFor(modelItem => item1.Coil_Defect1)
</td>
<td>
#Html.DisplayFor(modelItem => item1.Coil_Defect2)
</td>
<td>
#Html.DisplayFor(modelItem => item1.Coil_Defect3)
</td>
</tr>
}
</table>
</center>
</div>
<center>
Place Bid(s)
</center>
MyBids Page-->
#model NucorPrototypes_V1.ViewModel.MyBids
#{
ViewBag.Title = "MyBids";
}
Auction
Secondary Coil Auction
Bid Amount per 100 weight
Remove Bid
Serial Number
Minimum Bid
Current High Bid
Grade
Heat Number
Gauge
Width
Weight
Defect 1
Defect 2
Defect 3
#foreach( var row in Model.Coils)
{
Remove Bid
#Html.DisplayFor( modelItem => row.Coil_ID)
}
<center>
Confirm Bids
Save as...
</center>
for getting the result of multiple check boxes I use Request.Form
on your controller
Request.Form["chkBoxName"].ToString();
make sure every check box has the same name (chxBoxName in this example) and it will give you a list of all of the check boxes that were selected. You can then add those items to the next table on the controller and redirect there. Hope this helps
Edit:
this will return a comma delimited list of the selected check boxes. since you want to add them to another table you will need to query the database for them.
[HttpPost]
public ActionResult PostBack(){
Model model = new Model();
List<Bids> bids = new List<Bids>();
List<int> result = Request.Form["chkBoxName"].ToString().split(',');
foreach(var id in result){
//query the database to get the record for id
bids.add("result of query");
}
Model.Bids = bids;
return RedirectToAction("Bids", bids);
}
Just put the checkboxes in a sub form where the action goes to the target form. I don't see where the statement is in the HTML you provided, but you can have multiple forms on the same page with differing targets.
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.