Change DevExpress MVC ComboBox selected value based on another combobox value - c#

My code is below. I have 4 comboboxes, all in separate partial views, and I want to change the selected value of the county combobox when I select the judge combobox. The judge combobox has the county in the selection so you have the name or I can look up the county based on the judgeid How do I do that?
...
#model TestCascadeCombobox.Models.CCmaster
#{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<table>
<tr>
<td>
<div style="padding:5px">
#Html.Label("County", "County")
</div>
</td>
<td>
<div style="padding:5px">
#Html.Partial("CountyPartial", Model)
</div>
</td>
<td>
<div style="padding:5px">
#Html.Label("Judge", "Judge")
</div>
</td>
<td>
<div style="padding:5px">
#Html.Partial("JudgePartial", Model)
</div>
</td>
</tr>
<tr>
<td>
<div style="padding:5px">
#Html.Label("Year", "Year")
</div>
</td>
<td>
<div style="padding:5px">
#Html.Partial("YearPartial", Model)
</div>
</td>
<td>
<div style="padding:5px">
#Html.Label("Month", "Month")
</div>
</td>
<td>
<div style="padding:5px">
#Html.Partial("MonthPartial", Model)
</div>
</td>
</tr>
</table>
}
Next is the Partial View for the Judge
#model TestCascadeCombobox.Models.CCmaster
#Html.DevExpress().ComboBox(settings =>
{
settings.Name = "Judge";
//settings.CallbackRouteValues = new { Controller = "CircuitCaseloads", Action = "JudgePartial"};
settings.Properties.ValueType = typeof(int);
settings.Properties.TextField = "FullName";
settings.Properties.ValueField = "JudgeID";
settings.Properties.ClientSideEvents.SelectedIndexChanged = "function(s,e) { County.PerformCallback(); Month.PerformCallback(); }";
}).BindList(Model.judges).Bind(Model.judge).GetHtml()

The main idea it to handle the client-side SelectedIndexChanged event of the (judge) ComboBox and update the related (county) ComboBox via a callback. Check out the related MVC ComboBox Extension - How to implement cascaded combo boxes guide to find all the necessary implementation details.

Here is what I did to fix my problem.
function CountyComboBox_EndCallBack(s, e) {
s.SetValue(County.cpcountyid);
}
settings.Properties.ClientSideEvents.EndCallback = "CountyComboBox_EndCallBack";
settings.CustomJSProperties = (s, e) =>
{
e.Properties["cpcountyid"] = ViewData["countyid"];
};

Related

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

This table has a column with check boxs that need to select the row and send rows to another table.

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.

System.Web.Mvc.AjaxHelper does not contain a definition for 'BeginForm

System.Web.Mvc.AjaxHelper does not contain a definition for 'BeginForm i am getting this error in my mvc3 project please help me to resolve this
here is my code:
#{
ViewBag.Title = "Register";
Layout = "~/Views/Shared/_MyLayout.cshtml";
}
#model FoodMart.Models.AccountModel
#using (Ajax.BeginForm("Register", new AjaxOptions { UpdateTargetId = "result" }))
{
<div id="result" style="width:800px">
<fieldset style="width:100%">
<legend><span style="font-size:larger; color:Green; font-family:Arial">Register Here</span></legend>
<table style="width:100%">
<tr>
<td>#Html.LabelFor(m => m.users.FirstName,"Enter First Name")</td>
<td>#Html.TextBoxFor(m => m.users.FirstName, new { style = "width:150px" })</td>
<td><span style="font-size:medium; color:Green;">#Html.ValidationMessageFor(m=>m.users.FirstName)</span></td>
</tr>
<tr>
<td>#Html.LabelFor(m => m.users.Landline)</td>
<td>#Html.TextBoxFor(m => m.users.Landline, new { style = "width:150px" })</td>
</tr>
<tr>
<td><input type="submit" value="Register" /></td>
<td></td>
</tr>
</table>
</fieldset>
</div>
}
AjaxHelper.BeginForm is an extension method defined in the namespace System.Web.Mvc.Ajax.
Do you have this namespace defined in the web.config in your Views folder?
Have you tried inserting this instruction at the top of your view ?
#using System.Web.Mvc.Ajax

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