0

I have Customer class

public class Customer
{
    public string Name { get; set; }
    public string Surname{ get; set; }
    public string Email { get; set; }
    public string MobilePhone { get; set; }

    public int Type { get; set; }
}

In Index method of HomeController, I have created an instance of Customer and set values to properties.Then I stored it in session to get the object in Index(HttpPost) method which is in the same controller. In Index(HttpPost) method, I can get all the property values properly except 'Type' Property. When I get Customer object from session, 'Type' property of this object is always equal to 0.

public ActionResult Index(string id, string co)
{
    Customer cust = new Customer();
    cust.Name = "customer";
    cust.Surname = "test";
    cust.EMail = "[email protected]";
    cust.Type=2;

    Session["customer"] = cust;

    return View(customer);
}

[HttpPost]
public ActionResult Index()
{
    Customer customer = new Customer();
    if (Session["customer"] != null)
    {
        customer = (Customer)Session["customer"];
    }
    //customer.Type is equal 0
}

What could be the reason and how can it be avoided?

Thanks for replies in advance

3
  • Did you make sure that Session["customer"] was not null? Because if it was then you'll have that initial Customer that you created with all default values set to it's properties. Commented Aug 4, 2016 at 13:06
  • Yes. Other properties are not null or empty, they have the values which i set. Commented Aug 4, 2016 at 13:12
  • Your code looks totally fine to me. Are you very sure that this is the exact code in your project ? No types (in the session key) ? Commented Aug 4, 2016 at 13:13

1 Answer 1

1

Your code looks OK (except typos in cust.EMail -> cust.Email and return View(customer) -> return View(cust)) I even added this to my project and it works pretty well...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.