How to delete a single record from a list of records stored in a session - linq

I have the following code
here is how I add a list of values to session
public ActionResult Add(Product product)
{
if (Session["AddToCart"] == null)
{
Session["AddToCart"] = new List<Product>();
}
var list = (List<Product>)Session["AddToCart"];
list.Add(product);
}
but how to remove a single record when a session contains multiple records. I am trying to pass an Id but it is not removing the record from the session. Here is how I perform the next step.
Public ActionResult Remove(Product product)
{
Product prod=db.Products.Single(x=>x.Id==product.Id);
var list=(List<Product>)Session["AddToCart"];
//Is this the correct approach
list.Remove(prod);
}
The above code doesn't works. Am I correct or is there anything missing plz correct the above code. Thanks.

Try this,
var list=(List<Product>)Session["AddToCart"];
list.RemoveAll(p => p.Id == product.Id);
Your choice of finding the product with the code db.Products.Single(x=>x.Id==product.Id); may not be the same object with the one in the session.
Edit:
Or you can implement IEquatable<Product> interface. In this case your code would work too.
public class Product : IEquatable<Product>
{
public int Id;
public bool Equals(Product prod)
{
return prod.Id == Id;
}
// Rest of the class
}

Related

TempData not kept between postback

I need some advice on how to proceed with the mvc app I'm building. On my page I type out who is logged in to the page. This I first did by creating a base class where I created a user class which contained the users name and a image representing the user. Then I passed this class on to my views. But I also need to pass other models to my views depending on what view I'm in. Sure I could build a class that contain all different models I need to use on each page but there should be a easy way to pass name and image values across the pages and be persistant? I tried TempData together with TempData.Keep() but that was not persistant. How can I pass theses values between pages?
public ActionResult Validate(AccountModels.LoginModel model)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
var mu = _repo.GetUser(Membership.GetUser().ProviderUserKey.ToString());
TempData["Name"] = mu.Name;
TempData["Image"] = mu.Image;
TempData.Keep();
FormsAuthentication.RedirectFromLoginPage(model.UserName, model.RememberMe);
}
}
return View("Index");
}
As #Jyoti said, you could use of Keep() method.
To make it easy to work with TempData, I wrote these methods in my BaseController, and I use it in every controller when I need to transfer data between actions or between view and controller.
protected TReturnType GetTempDataValue<TReturnType>(PsmConstants.TempDataKey sessionName, bool peekData =false )
{
object value = peekData ? TempData.Peek(sessionName.ToString()) : TempData[sessionName.ToString()];
return (TReturnType) value;
}
protected void RemoveTempData(PsmConstants.TempDataKey sessionName)
{
if (TempData.ContainsKey(sessionName.ToString()) && TempData[sessionName.ToString()] == null) return;
TempData[sessionName.ToString()] = null;
}
protected void SetTempDataValue(PsmConstants.TempDataKey sessionName, object value)
{
if(TempData.ContainsKey(sessionName.ToString()))
TempData[sessionName.ToString()]=null;
TempData[sessionName.ToString()] = value;
}
protected void KeepTempDataValue(PsmConstants.TempDataKey sessionName)
{
if (TempData.ContainsKey(sessionName.ToString()))
TempData.Keep(sessionName.ToString());
}
And this is the Keys enumeration :
public enum TempDataKey
{
PageError = 1,
PageInfo = 2
}
And this is, the usage of these methods(Set value and Get value from TempData):
SetTempDataValue(PsmConstants.TempDataKey.PageError , 'your error message' );
var originalValues = GetTempDataValue<MyModel>(PsmConstants.TempDataKey.Info, true);
Use session instead of Temp if it is not working.but i think it should work.
TempData["Name"] = mu.Name;TempData["Image"] = mu.Image;TempData.Keep();
How you are passing this into other models,Please share the source code so that it will easy to identify.

MVC3 entity update issue

We have two models Person and Address. We have created one combined model of these two models as shown below.
public class Trust_Person_Master
{
public Person_Master PersonMaster { get; set; }
public Address_Master AddressMaster { get; set; }
public Trust_Person_Master()
{
}
public Trust_Person_Master(Person_Master personMaster, Address_Master addressMaster)
{
PersonMaster = personMaster;
AddressMaster = addressMaster;
}
}
Now, we use this Trust_Person_Master model, to save person and address.
So following is the method to save and edit.
[HttpPost, Ajax(true)]
public JsonResult SaveTrust(Trust_Person_Master entity)
{
int nCurrPersonId = entity.PersonMaster.Person_ID;
if (entity.PersonMaster.Person_ID > 0)
{
var update = db.Person.Find(entity.PersonMaster.Person_ID);
if (ModelState.IsValid)
{
TryUpdateModel(update);
}
}
else
{
db.Person.Add(entity.PersonMaster);
}
db.SaveChanges();
HttpContext.Application["TrustPersonSearch"] = null;
return Json(new { person_id = entity.PersonMaster.Person_ID, location_id = entity.PersonMaster.Location_ID });
}
But my problem is that when I edit person, i.e. just field of person master say first name, then that it executes the code as required without giving any error. But does not reflect the changes.
You need to show the TryUpdateModel() method. How are you updating the model ?
By looking at the code you have posted everything seems ok and should not give any problems, however I would like to see the way you are updating your entity, the problem seems to lie there.
Debug as much as possible and come back here with your findings.

LINQ-To-Sharepoint Multiple content types for a single list

I'm using SPMetal in order to generate entity classes for my sharepoint site and I'm not exactly sure what the best practice is to use when there are multiple content types for a single list. For instance I have a task list that contains 2 content types and I'm defining them via the config file for SPMetal. Here is my definition...
<List Member="Tasks" Name="Tasks">
<ContentType Class="LegalReview" Name="LegalReviewContent"/>
<ContentType Class="Approval" Name="ApprovalContent"/>
</List>
This seems to work pretty well in that the generated objects do inherit from WorkflowTask but the generated type for the data context is a List of WorkflowTask. So when I do a query I get back a WorkflowTask object instead of a LegalReview or Approval object. How do I make it return an object of the correct type?
[Microsoft.SharePoint.Linq.ListAttribute(Name="Tasks")]
public Microsoft.SharePoint.Linq.EntityList<WorkflowTask> Tasks {
get {
return this.GetList<WorkflowTask>("Tasks");
}
}
UPDATE
Thanks for getting back to me. I'm not sure how I recreate the type based on the SPListItem and would appreciate any feedback.
ContractManagementDataContext context = new ContractManagementDataContext(_url);
WorkflowTask task = context.Tasks.FirstOrDefault(t => t.Id ==5);
Approval a = new Approval(task.item);
public partial class Approval{
public Approval(SPListItem item){
//Set all properties here for workflowtask and approval type?
//Wouldn't there be issues since it isn't attached to the datacontext?
}
public String SomeProperty{
get{ //get from list item};
set{ //set to list item};
}
Linq2SharePoint will always return an object of the first common base ContentType for all the ContentTypes in the list. This is not only because a base type of some description must be used to combine the different ContentTypes in code but also it will then only map the fields that should definitely exist on all ContentTypes in the list. It is however possible to get access to the underlying SPListItem returned by L2SP and thus from that determine the ContentType and down cast the item.
As part of a custom repository layer that is generated from T4 templates we have a partial addition to the Item class generated by SPMetal which implements ICustomMapping to get the data not usually available on the L2SP entities. A simplified version is below which just gets the ContentType and ModifiedDate to show the methodology; though the full class we use also maps Modified By, Created Date/By, Attachments, Version, Path etc, the principle is the same for all.
public partial class Item : ICustomMapping
{
private SPListItem _SPListItem;
public SPListItem SPListItem
{
get { return _SPListItem; }
set { _SPListItem = value; }
}
public string ContentTypeId { get; internal set; }
public DateTime Modified { get; internal set; }
public virtual void MapFrom(object listItem)
{
SPListItem item = (SPListItem)listItem;
this.SPListItem = item;
this.ContentTypeId = item.ContentTypeId.ToString();
this.Modified = (DateTime)item["Modified"];
}
public virtual void MapTo(object listItem)
{
SPListItem item = (SPListItem)listItem;
item["Modified"] = this.Modified == DateTime.MinValue ? this.Modified = DateTime.Now : this.Modified;
}
public virtual void Resolve(RefreshMode mode, object originalListItem, object databaseObject)
{
SPListItem originalItem = (SPListItem)originalListItem;
SPListItem databaseItem = (SPListItem)databaseObject;
DateTime originalModifiedValue = (DateTime)originalItem["Modified"];
DateTime dbModifiedValue = (DateTime)databaseItem["Modified"];
string originalContentTypeIdValue = originalItem.ContentTypeId.ToString();
string dbContentTypeIdValue = databaseItem.ContentTypeId.ToString();
switch(mode)
{
case RefreshMode.OverwriteCurrentValues:
this.Modified = dbModifiedValue;
this.ContentTypeId = dbContentTypeIdValue;
break;
case RefreshMode.KeepCurrentValues:
databaseItem["Modified"] = this.Modified;
break;
case RefreshMode.KeepChanges:
if (this.Modified != originalModifiedValue)
{
databaseItem["Modified"] = this.Modified;
}
else if (this.Modified == originalModifiedValue && this.Modified != dbModifiedValue)
{
this.Modified = dbModifiedValue;
}
if (this.ContentTypeId != originalContentTypeIdValue)
{
throw new InvalidOperationException("You cannot change the ContentTypeId directly");
}
else if (this.ContentTypeId == originalContentTypeIdValue && this.ContentTypeId != dbContentTypeIdValue)
{
this.ContentTypeId = dbContentTypeIdValue;
}
break;
}
}
}
Once you have the ContentType and the underlying SPListItem available on your L2SP entity it is simply a matter of writing a method which returns an instance of the derived ContentType entity from a combination of the values of the base type and the extra data for the missing fields from the SPListItem.
UPDATE: I don't actually have an example converter class as we don't use the above mapping extension to Item in this way. However I could imagine something like this would work:
public static class EntityConverter
{
public static Approval ToApproval(WorkflowTask wft)
{
Approval a = new Approval();
a.SomePropertyOnWorkflowTask = wft.SomePropertyOnWorkflowTask;
a.SomePropertyOnApproval = wft.SPListItem["field-name"];
return a;
}
}
Or you could put a method on a partial instance of WorkflowTask to return an Approval object.
public partial class WorkflowTask
{
public Approval ToApproval()
{
Approval a = new Approval();
a.SomePropertyOnWorkflowTask = this.SomePropertyOnWorkflowTask;
a.SomePropertyOnApproval = this.SPListItem["field-name"];
return a;
}
public LegalReview ToLegalReview()
{
// Create and return LegalReview as for Approval
}
}
In either situation you would need to determine the method to call to get the derived type from the ContentTypeId property of the WorkflowTask. This is the sort of code I would normally want to generate in one form or another as it will be pretty repetitive but that is a bit off-topic.

How to update a List<T> in C#?

I'm taking a basic course in C# programming, have never programmed anything before. One of our exercises is to create a program that can rent out movies (i.e. a Videostore) from scratch.
One of my classes contains customers. I need a method where the user of the program can add customers to the first list and a separate method in which to display all customers, containing the newly added customers; or if no customers are added then the original ones.
This is what I´ve done so far:
I've created a List<T> for the original customers.
I have made a method that can add customers to the first list and display them in ONE method.
The problem is that I don´t know how to update the original list of customers with the ones the user adds. If I call the entire method it will obviously (even to me..) return the entire method and make the user add the customers over again. I´ve tried creating two List<T>s, but how can I make the original list update to include the customers the user adds?? I managed to call the first list from the second but the reverse doesn't work.
I have tried and tried and tried but I´ve simply run out of ideas! For me even getting this far has been quite the challenge. I thought about giving the whole thing up. Programming is not easy.
If anyone has any suggestions I would be very happy!
namespace MyNameSpace
{
public class Customers
{
public Customers()
{
}
public string Name
{
get;
set;
}
public string Tel
{
get;
set;
}
public List<Customers> CustomerList1() //Original customers
{
List<Customers> newCustomer = new List<Customers>
{
new Customers
{
Name="A",
Tel="1"
},
new Customers
{
Name="H",
Tel="2"
},
};
return newCustomer;
}
public List<Customers> CustomerList2() //User adds new customers
{
List<Customers> custList = CustomerList1();
Console.WriteLine("---------------------------");
Console.WriteLine("New Customer");
Console.WriteLine("---------------------------");
Console.WriteLine("Name:");
Console.WriteLine("Tel:");
List<Customers> addedCustomer = new List<Customers>
{
new Customers //There is most likely a better way...
{
Name=Console.ReadLine(),
Telephone=Console.ReadLine()
}
};
custList.AddRange(addedCustomer);
Console.WriteLine("***************List******************");
foreach (Customers c in custList)
{
Console.WriteLine();
Console.WriteLine(c.Name);
Console.WriteLine(c.Tel);
Console.WriteLine();
}
Console.WriteLine("******************************************");
return addedCustomer;
}
public void CustomerView() //This method only returns original list
{
List<Customers> customers = CustomerList1();
foreach (Customers c in customers)
{
Console.WriteLine();
Console.WriteLine(c.Name);
Console.WriteLine(c.Tel);
Console.WriteLine();
}
Console.WriteLine("*******************");
}
public void CustomerListAdd() //This is another method I´ve tried to add
{ customers..
List<Customers> customers = CustomerList1();
Console.WriteLine("");
Console.WriteLine("---------------------------");
Console.WriteLine("New Customer");
Console.WriteLine("---------------------------");
Customers customerAdd = new Customers();
Console.WriteLine("Name:");
customerAdd.Name = Console.ReadLine();
Console.WriteLine("Tel:");
customerAdd.Telephone = Console.ReadLine();
customers.Add(customerAdd);
Console.WriteLine();
foreach (Customers c in customers)
{
Console.WriteLine();
Console.WriteLine(c.Name);
Console.WriteLine(c.Tel);
Console.WriteLine();
}
Console.WriteLine("*******************");
}
}
}
Thank you in advance
Make your Customer list class level:
public class MyClass
{
// this is outside of a method, but inside the class
private List<Customer> customers;
public MyClass()
{
// instantiate the customer list inside the constructor
customers = new List<Customer>();
// add a default customer to the list by calling the AddCustomer
// method in the constructor.
AddCustomer(new Customer() { Name = "A", Tel="1" });
// You can also bypass the AddCustomer method below and just call
// customers.Add() here instead. If you have other things you want
// to do (like insert the customer into a database, for example)
// you might want to keep the method like I have below.
}
public void AddCustomer(Customer cust)
{
// add the customer to the existing list.
customers.Add(cust);
}
}

LINQ2SQL Entities - Updating only the fields that have changed

I was hoping there was an easier way to do this in my MVC 3 project. In my database, I have a Customer table that is mapped in my application via LINQ2SQL. There is also a partial customer class where I perform updates, look-up etc - which where I have an update method like this:
public static void Update(Customer customer)
{
if (customer == null)
return;
using(var db = new DataBaseContext)
{
var newCustomer = db.Customers.Where(c => c.customer_id = customer.customer_id).SingleOrDefault();
if(newCustomer == null)
return;
newCustomer.first_nm = customer.first_nm;
// ...
// ... Lot's of fields to update
// ...
newCustomer.phone_num = customer.phone_nm;
db.SubmitChanges();
}
}
What I was hoping to find was a less-cumbersome method to update the fields in newCustomer with the corresponding fields in customer that are different.
Any suggestions? Thanks.
I think you can implement IEqualityComparer:
public class Customer
{
public string first_nm { get; set; }
public int phone_num { get; set; }
}
class CustomerComparer : IEqualityComparer<Customer>
{
public bool Equals(Customer x, Customer y)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
//Check whether the customer' properties are equal.
return x.first_nm == y.first_nm && x.phone_num == y.phone_num ;
}
}
and do it as follows:
if (newCustomer != customer)
{
myDbContext.Customers.Attach(customer,true); // true means modified.
}
Or implement ICloneable and set newCustomer to customer.Clone(). then there's no need to attach customer since newCustomer is already attached.
in EF(4.1), I think You just have to attach the entity as modified:
myDbContext.Customers.AttachAsModified(customer, this.ChangeSet.GetOriginal(customer), myContext);
UPDATE:
Well, it seems like L2S needs original values of the entity. In reply to your comment, you have a couple choices: Using a timestamp column, returning a subset of entities, or having the original entity in your hand. In your scenario, you have the original entity already:
// This is your original entity
var newCustomer = db.Customers.Where(c => c.customer_id = customer.customer_id).SingleOrDefault();
So you will most probably can do:
if (customer != newCustomer)
{
myDbContext.Customers.Attach(customer, newCustomer);
}
Note: I'd rename newCustomer to originalCustomer if I were you since it's more related to the entity's state.
The problem with this approach is that you have an extra trip to database to get your original customer (newCustomer in your code). Take a look at here, here and definitely here to see how you can use TimeStamp columns to prevent the extra database trip.

Resources