The DB has 4 columns; ID Device SubDevice and Unit
I have a dropdown list which sends the selected item back to the post method in the controller. In that controller I want to run a query against the database using the selected value from the dropdown list, i.e. the ID number.
So if the user selects ID 1, the query will retrive the Unit value and then I can perform additional operations on that data beforing displaying it to the user.
Im not sure if thats correct, but im trying to get the returned value from the query to be assigned to a variable which I am sending back to the view via a bag.
@using (Html.BeginForm())
{
<fieldset>
<div class="display-label">
How many devices do you have?
</div>
<div class="display-field">
<input type="text" id="amount" name="amount" value="1"/>
</div>
<div class="display-label">
What type of device do you have?
</div>
<div class="editor-field">
@Html.DropDownList("ID", "--Select Device--")
@Html.ValidationMessageFor(model => model.ID)
</div>
<div class="display-label">
How much data do you need to transfer?
</div>
<div class="display-field">
<input type="text" id="data" name="data" value="1"/>GB
</div>
@*For Diagnostics*@
@ViewData["calc_output"]
@ViewData["amount"]
@ViewData["data"]
@ViewData["tempvar"]
<p>
<input type="submit" value="Calculate" />
</p>
</fieldset>
}
And in the controller;
[HttpPost]
public ActionResult Index(string ID, string device, int amount, int data, int unit = 0, double cost = 0)
{
int selected_item = 0;
string tempvar;
var db = new CalculatorDBContext();
if(int.TryParse(ID, out selected_item))
{
var query_search = db.Calculator.Where(c => c.ID == selected_item);
tempvar = query_search.ToString();
ViewBag.something = tempvar;
}
if (selected_item == 0)
{
ViewBag.calc_output = "Select a device first!";
}
else
{
ViewBag.calc_output = "You selected" + device + "which costs" + unit + "/item";
cost = (unit * amount) + (data * 0.5);
ViewBag.calc_output2 = "The estimated cost for this jobs is $" + cost;
//Debugging use
ViewBag.data = data;
ViewBag.amount = amount;
ViewBag.tempdata = selected_item;
ViewBag.tempdata1 = unit;
ViewBag.tempdata2 = device;
}
var query = db.Calculator.Select(c => new { c.ID, c.device, c.unit });
ViewBag.ID = new SelectList(query.AsEnumerable(), "ID", "device", "unit");
return View();
}
So what im trying to do is get the selected items ID then run a query agaist the DB which will get the unit value for that ID.
So for example, a Hammer which has ID 1 and a unit of 10.
After I have the unit value, I perform some calculations with the value and sent it back to the view.
DJ_Mo_G
Member
5 Points
42 Posts
MVC4 + Entity + MSSQL Query
Feb 01, 2013 08:10 AM|LINK
Hi,
I need to query a database I have, the following code is representative of what I want to achieve;
<somevariable> is a passed variable from a different module in the controller.
My current project uses entity to communicate with SQL, how would I go about creating the MVC/Entity code to perform the above query?
There is probably information missing which might be required, if so please specify what I need to provide.
Thanks :)
ajinder
Member
114 Points
31 Posts
Re: MVC4 + Entity + MSSQL Query
Feb 01, 2013 09:28 AM|LINK
May Be this will help u.....
(From p in obj.entityclassname where ID=<somevariable> select P).SingleOrDefault();
sandeepsacha...
Member
68 Points
29 Posts
Re: MVC4 + Entity + MSSQL Query
Feb 01, 2013 10:30 AM|LINK
What are you trying to expect from the query? a list or single result that you must specify.
Website: www.sandeepsachan.com
DJ_Mo_G
Member
5 Points
42 Posts
Re: MVC4 + Entity + MSSQL Query
Feb 01, 2013 11:42 AM|LINK
Hi Sandeepsachan,
The DB has 4 columns; ID Device SubDevice and Unit
I have a dropdown list which sends the selected item back to the post method in the controller. In that controller I want to run a query against the database using the selected value from the dropdown list, i.e. the ID number.
So if the user selects ID 1, the query will retrive the Unit value and then I can perform additional operations on that data beforing displaying it to the user.
Hope that makes sence :)
ajinder
Member
114 Points
31 Posts
Re: MVC4 + Entity + MSSQL Query
Feb 01, 2013 11:50 AM|LINK
By Default it returns Collection So Use SingleOrDefault() method ........
ur Controller Action Result Looks LIke....
var q= (From p in obj.CalCulators where p.Id==<someVariable> select P).SingleOrdefault();
return View(q);
DJ_Mo_G
Member
5 Points
42 Posts
Re: MVC4 + Entity + MSSQL Query
Feb 01, 2013 12:29 PM|LINK
Hi Ajinder,
I think i may be mistaken in what i said previously,
My current code to retrive data from the database is as follows;
var db = new CalculatorDBContext(); var query = db.Calculator.Select(c => new { c.ID, c.device });Is there something silimar I can use to implement the query I want as originally posted?
ajinder
Member
114 Points
31 Posts
Re: MVC4 + Entity + MSSQL Query
Feb 01, 2013 12:47 PM|LINK
var query = db.Calculator.Where(c=>c.ID==<variableUgetDuringPOST>);
OR
Var Query=db.Calculator.Find(<variableUgetDuringPOST>);
Hopes I got Ur Problem & This Helps U.....
DJ_Mo_G
Member
5 Points
42 Posts
Re: MVC4 + Entity + MSSQL Query
Feb 02, 2013 01:49 AM|LINK
Hi Ajinder,
This is exactly what I wanted,
Im using the code you provided as follows;
var query_search = db.Calculator.Where(c => c.ID == selected_item); tempvar = query_search.ToString(); ViewBag.something = tempvar;Im not sure if thats correct, but im trying to get the returned value from the query to be assigned to a variable which I am sending back to the view via a bag.
ajinder
Member
114 Points
31 Posts
Re: MVC4 + Entity + MSSQL Query
Feb 02, 2013 07:24 AM|LINK
Please Show ur View Coding So i better What u trying to do......
Also instead of ViewBag u can return Query_search to view to make it strongly typed view makes it easy....
DJ_Mo_G
Member
5 Points
42 Posts
Re: MVC4 + Entity + MSSQL Query
Feb 02, 2013 02:36 PM|LINK
Here is the View code;
@using (Html.BeginForm()) { <fieldset> <div class="display-label"> How many devices do you have? </div> <div class="display-field"> <input type="text" id="amount" name="amount" value="1"/> </div> <div class="display-label"> What type of device do you have? </div> <div class="editor-field"> @Html.DropDownList("ID", "--Select Device--") @Html.ValidationMessageFor(model => model.ID) </div> <div class="display-label"> How much data do you need to transfer? </div> <div class="display-field"> <input type="text" id="data" name="data" value="1"/>GB </div> @*For Diagnostics*@ @ViewData["calc_output"] @ViewData["amount"] @ViewData["data"] @ViewData["tempvar"] <p> <input type="submit" value="Calculate" /> </p> </fieldset> }And in the controller;
[HttpPost] public ActionResult Index(string ID, string device, int amount, int data, int unit = 0, double cost = 0) { int selected_item = 0; string tempvar; var db = new CalculatorDBContext(); if(int.TryParse(ID, out selected_item)) { var query_search = db.Calculator.Where(c => c.ID == selected_item); tempvar = query_search.ToString(); ViewBag.something = tempvar; } if (selected_item == 0) { ViewBag.calc_output = "Select a device first!"; } else { ViewBag.calc_output = "You selected" + device + "which costs" + unit + "/item"; cost = (unit * amount) + (data * 0.5); ViewBag.calc_output2 = "The estimated cost for this jobs is $" + cost; //Debugging use ViewBag.data = data; ViewBag.amount = amount; ViewBag.tempdata = selected_item; ViewBag.tempdata1 = unit; ViewBag.tempdata2 = device; } var query = db.Calculator.Select(c => new { c.ID, c.device, c.unit }); ViewBag.ID = new SelectList(query.AsEnumerable(), "ID", "device", "unit"); return View(); }So what im trying to do is get the selected items ID then run a query agaist the DB which will get the unit value for that ID.
So for example, a Hammer which has ID 1 and a unit of 10.
After I have the unit value, I perform some calculations with the value and sent it back to the view.
Hope this clears up what Im trying to do.