So I am deeply confused. I have two tables, one is locations assignments which consists of: location id, type and type id. I also have a table called services, which consists of name, id , description and icon.
The idea is to say, get me back all 13 services, from that we create 13 checkboxes. then we say, check the location assignments table, if this services (based on type, id and location id) matches a service in that list, check the checkbox, else leave it unchecked.
What I ahve so far is:
public static IEnumerable<Constants.Assignable> getAllService(int id)
{
List<Constants.Assignable> assign = new List<Constants.Assignable>();
using (var db = new Context())
{
var serv = from s in db.Services
join la in db.LocationAssignments on s.id equals la.typeId into LocationAssignments
from la in LocationAssignments
where la.locationId == id && s.id == la.typeId && la.type == Constants.SERV
select s;
foreach(var s in serv)
{
assign.Add(new Constants.Assignable(){
id = s.id, name = s.name
});
}
return assign;
}
}
which returns me, currently, two services, when it should return me 13. So there is something wrong with my join.
from there we do:
<h3 class="muted">Services Nearby</h3>
IEnumerable<UFA.Location.Core.Constants.Assignable> ServicesNearby = UFALocationApp.Helpers.LocationHelper.QueryHelper.getAllServicesNearby(Model.id);
foreach (var servicenb in ServicesNearby)
{
<div class="control-group">
<label class="control-label" for="serviceNearBy">
#servicenb.name
</label>
<div class="controls">
<input type="checkbox" id="Locationservice" value="#servicenb.id" name="serviceNB" checked="#(servicenb.assigned ? "checked" : "")" />
</div>
</div>
}
which prints out the two check boxes that are, in this case checked. there should be 11 more that are unchecked.
What do I have to change in my query to say: get me all services and only check off the ones associated with this location?
To make it a LEFT JOIN, you need to use DefaultIfEmpty(), it seems that the component you're missing to make this work;
var serv = from s in db.Services
join la in db.LocationAssignments on s.id equals la.typeId
into LocationAssignments
from la in LocationAssignments.DefaultIfEmpty()
where la.locationId == id && s.id == la.typeId
&& la.type == Constants.SERV
select s;
If I read you well you're after something like this:
var assign = (from s in db.Services
select new Constants.Assignable
{
id = s.id,
name = s.name,
checked= db.LocationAssignments.Any(la => la.typeId == s.id)
}).ToList();
Now you can change the value of checked by clicking the checkbox and process the changes when you post back.
Related
In my ASP.NET MVC application i've been trying to select the data from the table from users that are ACTIVE, which is on a column from the database as true or false, however when i try to do this code, it doesn't show any data:
com.CommandText = "SELECT [NumMec], [NomeCompleto] FROM [CORE_USERS] WHERE [Activo] like 'T%'";
I've tried a different approach, which was controlling the select by doing a (Data.Activo == "True") in the View. But I don't want this approach, I want to control the data from the database:
#{
if (Model != null)
{
foreach (var Data in Model)
{
if (Data.Activo == "True")
{
<tr>
<td>#Data.NumMec</td>
<td>#Data.Activo</td>
<td>#Data.NomeCompleto</td>
</tr>
}
}
}
}
if you are on sql server and datatype of Activo column is bit, then the correct syntax is :
SELECT [NumMec], [NomeCompleto] FROM [CORE_USERS] WHERE [Activo] = 1
bit = 1 means true
So, I have a DataTable with left fixed column but wanted to add a Select like filter, which is not supported by datatables.
I created the select filter and it works just fine withouth the fixed column as it filters from the original table. The thing is when I fix the left column, it doesn't apply the filter into the cloned table
I've tried adding and 'id' into the cloned table and then applying the filter into it too, but it won't work
As it is seen in the picture, the filter 'Externas' it's applied and should only show the orange rows, but the fixed columns shows every row
Fixed column not filtering
This is my select
<select id="filtrarTipo" onclick="filtradoTipo(); filtradoTipoC();">
<option value="" selected disabled>Seleccione</option>
<option value="">Todas</option>
<option value="Interna">Interna</option>
<option value="Externa">Externa</option>
</select>
And my jquery
function filtradoTipo() {
// Declare variables
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById("filtrarTipo");
filter = input.value.toUpperCase();
table = document.getElementById("dtBecas");
tr = table.getElementsByTagName("tr");
// Loop through all table rows, and hide those who don't match the search query
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[4];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
I am trying to query the country column from one of my table and trying to group them so that one country name appears only once.
The controller code is
ViewBag.Countries = (from pT in _context.InfoProducts
group pT by new { pT.Country } into g
select new
{
Country = g.Key.Country
}).ToList();
In my view I am trying to show this list of countries in a dropdown like this
<div class=" form-group">
#Html.Label("Country")
<div class="col-md-12 ">
#Html.DropDownList("Country", new SelectList(ViewBag.Countries, "Country"), "Select Country", new { #class = "col-form-label col-md-12 label-align" })
</div>
</div>
The problem I am facing is though it is returning the expected names of the countries, the result is generating as a key, value pair something like
{Country = USA}
{Country = Canada}
The HTML generated copied from inspect element is given below
<select class="col-form-label col-md-12 label-align" id="Country" name="Country">
<option value="">Select Country</option>
<option>{ Country = USA}</option>
<option>{ Country = Canada}</option>
</select>
How can I get only the country name in the dropdown instead of the result I am currently getting.
Rather than using group by, it sounds like distinct would be a better fit:
_context.InfoProducts.Select(x => x.Country).Distinct().ToList();
This will give you back a list of strings to use, which won't give you the key-value pair problem you're currently having.
One more thing, when you use new SelectList(ViewBag.Countries, "Country") this overload of SelectList's constructor will try to set the value of Country as the selected item, but this doesn't exist in your list, so Select Country will always appear as selected. If you want that to select an actual country, then it would have to look something like:
new SelectList(ViewBag.Countries, "Canada")
where you would fetch Canada from the data for the current product.
I am currently facing a dilemma within the project I am currently completing. I have a database where two tables are linked through foreign keys which are of data type int.
What I am trying to do is retrieve a value from tblProductColour and using the INNER JOIN function add the information to the other table which is tblProducts.
Here is the controller code,
public ActionResult Index()
{
var db = WebMatrix.Data.Database.Open("Database");
if (Session["AdministrationTeam"] != null)
{
View = "ViewAllProducts";
}
else
{
return RedirectToAction("Login", "Home");
}
return View(View, odb.tblproducts.SqlQuery("SELECT * FROM tblproducts t INNER JOIN tblProductColour ot ON t.ID = ot.ProductColour"));
}
The getters and setters that are stored within the model itself are as shown below,
public int? ProductColour {get;set;}
Here is the code I am using in my View to retrieve the information from the model,
#foreach (var item in Model) {
<div class="col-lg-3 col-md-6 col-sm-6 ">
<img src="#item.ProductImagePath" alt="Image Should Show Here" style="margin:auto auto; background-color:#fff;" width="100%" height="20%"/>
<p>Product Id: #Html.DisplayFor(modelItem => item.Id) </p>
<p> Product Name: #Html.DisplayFor(modelItem => item.ProductName)</p>
<p>Supplier: #Html.DisplayFor(modelItem => item.ProductSupplier) </p>
<p>Colour: #Html.DisplayFor(modelIteem => item.ProductColour) </p>
<p>Product Quantity: #Html.DisplayFor(modelItem => item.ProductQuantity)</p>
<p> Product Description: #Html.DisplayFor(modelItem => item.ProductDescription)</p>
</div>
When retrieving the information, I am faced with this error shown below,
Conversion failed when converting the varchar value 'Red' to data type int.
Here are the entities,
tblproducts
tblProductColour
If anyone has any solutions to how I can display the information it would be appreciated.
You're joining the wrong items
"SELECT * FROM tblproducts t INNER JOIN tblProductColour ot ON t.ID = ot.ProductColour"
should be
"SELECT * FROM tblproducts t INNER JOIN tblProductColour ot ON t.ProductColour = ot.ID"
You then need to change
#Html.DisplayFor(modelIteem => item.ProductColour)
in your view to (assuming the correct reference name for the table is tblProductColours) to
#Html.DisplayFor(modelIteem => item.tblProductColours.ProductColour)
- or
#Html.DisplayFor(modelIteem => item.ProductColours.ProductColour) -
may also work - just depends on how it is referenced, hopefully autocomplete will help you out.
I want to select the record from the list box using text. how can i use the filter function to select the particular record. I will be having many options but i want to select the value which i want by checking the text (e.g Spanish). I dont want to select value by index becoz if i do that i wont be able to verify test, moreover list gets updated. kindly help. below r my html code.
<ul class="addList">
<li ng-repeat="skill in availableSkills" ng-click="addSkillFunc(skill, $index)" class="ng-binding ng-scope">Mandarin</li>
<li ng-repeat="skill in availableSkills" ng-click="addSkillFunc(skill, $index)" class="ng-binding ng-scope">English</li>
<li ng-repeat="skill in availableSkills" ng-click="addSkillFunc(skill, $index)" class="ng-binding ng-scope">Spanish</li>
</ul>
Yea i can select the record by index. but i want something like selectbyvisibleText which is available in Selenium.
Finally got the solution. created a function SelectRowByCellValue and used to call it where ever i want by
SelectRowByCellValue(AGP.SkillList, Data.SkillSelect);
SkillList = element.all(by.css('Ul.addList li'));
SkillSelect = Value that u want to select. (Spanish)
this.SelectRowByCellValue = function (Elem, Texts) {
Elem.filter(function (element) {
return element.getText().then(function (text) {
if (text == Texts) {
element.click();
return false;
}
});
}).then(function (filteredElements) {
});
};