MVC设定值到整数对象的属性在会话
问题描述:
我有顾客类MVC设定值到整数对象的属性在会话
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; }
}
在HomeController中的索引方法,我已经创建客户的实例,并且设置值,以properties.Then我其存储在会话在同一控制器中的Index(HttpPost)方法中获取对象。在索引(HttpPost)方法中,我可以正确地获取除“类型”属性以外的所有属性值。当我从会话中获取Customer对象时,此对象的'Type'属性始终等于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
}
可能是什么原因以及如何避免?
感谢答复提前
答
你的代码看起来OK(除cust.EMail
错别字 - >cust.Email
和return View(customer)
- >return View(cust)
)我甚至已将此添加到我的项目,它工作得很好......
难道你确定'Session [“customer”]'不是'null'?因为如果是这样,那么您将拥有使用所有默认值设置为其属性的初始“客户”。 – juharr
是的。其他属性不为null或为空,它们具有我设置的值。 –
你的代码对我来说看起来非常好。你确定这是你项目中的确切代码吗?没有类型(在会话密钥中)? – Shyju