Remove Dropdownlist Value - asp.net-mvc

I am working in MVC2. Here i had Employee Screen. There itself i am having a dropdown list. In that all the Employee Names will loaded. The Employee profile which i am viewing should not be loaded in the dropdown list. I should remove the particular Employee from the dropdown list. Here is my code for loading dropdown...How to do this...
Dictionary<string, Employee> Employee1 = new Dictionary<string, EmployeeDetails>();
Employee1 = EmployeeProxy.GetPrimaryEmployeeList(UserIdentity.TenantID);
List<EmployeeDetails> managerDetailsList = Employee1.Values.ToList();
if (managerDetailsList != null && managerDetailsList.Count > 0)
{
managerDetailsList.Sort(delegate(EmployeeDetails p1, EmployeeDetails p2) { return p1.FirstName.CompareTo(p2.FirstName); });
}
foreach (EmployeeDetails employeedetails in managerDetailsList)
{
employeedetails.FirstName = employeedetails.FirstName + " " + employeedetails.LastName;
}

RobinHood,
Simply change this line:
List<EmployeeDetails> managerDetailsList = Employee1.Values.ToList();
to:
List<EmployeeDetails> managerDetailsList = Employee1.Values.Where(x => x.ID != Employee1.ID).ToList();
assuming that such an attribute (ID) exists. Basically, what i'm saying is that from the managerDetailsList, exclude the Employee1 member (based on the assumption that Employee1.Values is IQueryable).

Related

ASP.NET MVC 5, Storing list values in Session

I am trying to store list of products in List<> which I have stored in session, but when I add second product it only shows first in my View page.
..............
List<ShoppingCartItem> ShoppingCartItems = new List<ShoppingCartItem>
{
new ShoppingCartItem() {Product = product.Name, Attributes = atts, Options = opts, Price = producttotalprice, Quantity = 1}
};
if (Session["Cart"] == null)
{
Session["Cart"] = ShoppingCartItems;
}
return View(Session["Cart"]);
}
Anybody can help me please to retrieve all products I have stored.
You're creating a new List<ShoppingCartItem> every time and only ever putting one element in that list. It sounds like you want to first check if there's already a list in the session. And, if so, add the new element to that list. Something like this:
List<ShoppingCartItem> shoppingCartItems;
if (Session["Cart"] != null)
{
shoppingCartItems = (List<ShoppingCartItem>)Session["Cart"];
}
else
{
shoppingCartItems = new List<ShoppingCartItem>();
}
shoppingCartItems.Add(new ShoppingCartItem() {Product = product.Name, Attributes = atts, Options = opts, Price = producttotalprice, Quantity = 1});
Session["Cart"] = shoppingCartItems;
return View(shoppingCartItems);
If that is the "add" code, you're not actually amending the list. You're declaring an entirely new ShoppingCartItems list which contains the new product.
It works for the first product because this returns true:
if (Session["Cart"] == null)
The second time around this is false, and nothing happens. What you want to do is:
1) Retrieve the session cart which is a 'List' (if it is null, then initialise a new one.
2) Create a new ShoppingCartItem from the product which has been passed to the controller. Add that product to the cart.

MVC - Linq to SQL JOINS

I have a controller that accepts a list of strings. THese strings essentially are IDs that a user selects on the view. I need to build the model based upon fields from to tables, hence the need for the join. The bellow code will not build as it claims the properties from the joined table do not exist. It only accepts table 1 values. Item.Well_No and Item.Well_Name throw the error. These are included in the "y" table that i joined to "x"..
[HttpPost]
public ActionResult buildSelectionTable(List<string> dta)
{
var a = from x in db._AGREEMENTS
join y in db.WELL_AGMT_XREF on x.AGMT_NUM equals y.AGMT_NUM
where dta.Contains(x.AGMT_NUM)
select x;
List<AgmtModel> model = new List<AgmtModel>();
foreach (var item in a)
{
model.Add(new AgmtModel { Agmt_Name = item.AGMT_NAME, Agmt_Num = item.AGMT_NUM, Agmt_Type = item.AGMT_TYPE_DESCR, Amnt_Status = item.AGMT_STAT_DESCR, Company = item.CO_NAME, DaysToExp = item.DaysToExp, Drs_Url = item.DRS_URL, Effective_Date = item.EFF_DT, Orig_Lessee = item.ORIG_LESSEE, Prop_Status = item.AGMT_PROP_STAT_DESCR, Expiration_Date = item.EXPR_DATE, Acreage = item.LGL_AREA, Extention_Expiration = item.EXTN_EXPR_DT, WellNo = item.WELL_NO, Well_Name = item.WELL_NAME });
}
return PartialView("_SelectionTable", model);
}
You are only selecting x in your query you need to also select y and reference it.
change select x to be select new { x, y}
and then
foreach (var item in a)
{
model.Add(new AgmtModel { Agmt_Name = item.y.AGMT_NAME, Agmt_Num = item.x.AGMT_NUM ... });
}
you need to insert .x or .y before you the field to determine the field names
alternatively you could actually put the constructor directly in the query
so instead of select x
select new AgmtModel { Agmt_Name = y.AGMT_NAME, etc...}
then you can just return PartialView("_SelectionTable", a.ToList())

How do I remove the foreach from this linq code?

I'm fairly new at MVC and linq and viewmodels in particular. I managed to get a create and index views to work. The "insert" wasn't as hard as the "list".
I have this linq query:
public ActionResult Index()
{
List<BlendElVM> BEVM = new List<BlendElVM>();
var list = (from Blend in db.blends
join BlendEl in db.blendEl on Blend.ID equals BlendEl.ID
select new
{
Blend.ID, Blend.Title, Blend.TransDt, BlendEl.Comment
}).ToList();
foreach (var item in list)
{
BlendElVM o = new BlendElVM(); // ViewModel
o.Comment = item.Comment;
o.Title = item.Title;
o.TransDt = item.TransDt;
o.ID = item.ID;
BEVM.Add(o);
}
return View(BEVM);
}
What I'm not sure about is the "foreach" section. When I'm running in debug, the "list" shows up fine, but if I comment out the "foreach" I get an error - ie not expecting the model. What does the foreach do? It has to do with the database, but I don't understand the where it is using the "o" and setting the columns. I thought it would all be in one linq query. Is it possible to combine the two and eliminate the "foreach"?
var BEVM = (from blend in db.blends
join BlendEl in db.blendEl on Blend.ID equals BlendEl.ID
select new BlendELVM
{
ID = blend.ID,
Title = blend.Title,
TransDT = blend.TransDt,
comment = blendEl.Comment
}).ToList();
I believe that the foreach is needed in order to read every element in the object so in this case you have:
BlendElVM o = new BlendElVM();
So you're creating and object named " o " of the type BlendELVM and this object contains all the attributes that you declared before which are: ID, Title, TransDT, etc
When you put:
foreach (var item in list)
{
BlendElVM o = new BlendElVM(); // ViewModel
o.Comment = item.Comment;
o.Title = item.Title;
o.TransDt = item.TransDt;
o.ID = item.ID;
BEVM.Add(o);
}
You're assigning to the new object o the item that you're reading in the list and in the end adding it to the BVEM list and answering if you can combine them i will say no because at first you're declaring the query and then you're reading the items on the list and assining them to the BEVM list

Writing a Distinct LINQ query with a list

I have the following LINQ query to fill up my model.
var blogs = (from b in Context.Blogs
select new BlogTreeView
{
Created = EntityFunctions.TruncateTime(b.Created),
Children = (from ba in Context.Blogs
where EntityFunctions.TruncateTime(ba.Created) == EntityFunctions.TruncateTime(b.Created)
select new BlogTitle
{
ID = ba.ID,
Title = ba.Title
})
}).Distinct();
The problem is that the distinct gives the following error:
"The 'Distinct' operation cannot be applied to the collection ResultType of the specified argument.\r\nParameter name: argument"
I also tried this:
var blogs = (from b in Context.Blogs
select new BlogTreeView
{
Created = EntityFunctions.TruncateTime(b.Created)
}).Distinct();
This gives me only the unique dates like I want.
Then I've tried to add the childrens to the model with the help of a foreach:
foreach (var item in blogs)
{
item.Children = (from ba in Context.Blogs
where
EntityFunctions.TruncateTime(ba.Created) ==
EntityFunctions.TruncateTime(item.Created)
select new BlogTitle
{
ID = ba.ID,
Title = ba.Title
});
}
But then my return value is null for the children list. In my foreach loop the Children list has the values that I want but not in the return field.
What am I doing wrong, and why did the first query gave me that error?

Issue With Showing Distinct fields using Linq

var getAllProducts = _productService.GetAllProducts();
if (productstest.Count > 0)
{
model.idproduct.Add(new SelectListItem()
{
Value = "0",
Text = _localizationService.GetResource("Common.All")
});
foreach (var m in getAllProducts)
model.idproduct.Add(new SelectListItem()
{
Value = m.Id.ToString(),
**Text = m.Size.Distinct().ToString(),**
Selected = model.Pid == m.Id
});
}
public virtual IList<Product> GetAllProducts(bool showHidden = false)
{
var query = from p in _productRepository.Table
orderby p.Name
where (showHidden || p.Published) &&
!p.Deleted
select p;
var products = query.ToList();
return products;
}
The issue is even i tried to populate the select list with distinct size using: Text = m.Size.Distinct().ToString(), but it shows the duplicate for instance 100 products are of size 33 cm , the list will populate the dropdownlist in the view with 33cm occuring 100 times , I dont want to show 100 times , just want to show 1 time, Can any one assist me with this issue ?
Presumably you are only trying to show one product of each different size... if so initialising your getAllProducts variable like so will do the trick:
var getAllProducts = _productService.GetAllProducts().GroupBy(p => p.Size).Select(g => g.First());

Resources