Guid在MVC4中没有正确传递?

时间:2015-12-17 11:17:13

标签: javascript c# jquery twitter-bootstrap asp.net-mvc-4

您的GUID未正确传递。

我的表格

客户

CustomerID uniqueidentifier primarykey not null,

DisplayName varchar(100)

PrintName varchar(100)

CustomerAddress表

CustomerAddressID uniqueidentifier primarykey not null,

CustomerID uniqueidentifier null,

AddressID uniqueidentifier null

地址

AddressID uniqueidentifier primarykey not null,

DisplayName varchar(100)

Street varchar(100)

位置varchar(100)

AreaID Uniqueidentifier null,

Pincode varchar(100)

我的模特

public class CustomerModel1
    {
        public System.Guid CustomerID { get; set; }
        public string DisplayName { get; set; }
        public string PrintName { get; set; }
}

public partial class CustomerAddressModel
    {
        public System.Guid CustomerAddressID { get; set; }
        public Nullable<System.Guid> CustomerID { get; set; }
        public Nullable<System.Guid> AddressID { get; set; }

   }

 public partial class AddressModel
    {
        public System.Guid AddressID { get; set; }
        public string DisplayName { get; set; }
        public string Street { get; set; }
        public string Location { get; set; }
        public Nullable<System.Guid> AreaID { get; set; }
        public string PinCode { get; set; }
   }

我的ViewModel

   public class CustomerViewModel
   {
    public System.Guid CustomerID { get; set; }
    public string CustomerName { get; set; }
    public System.Guid CustomerAddressID { get; set; }
    public System.Guid AddressID { get; set; }
    public string Address { get; set; }
    public string Street { get; set; }
    public string Location { get; set; }
    public Nullable<System.Guid> AreaID { get; set; }
    public string PinCode { get; set; }
 }

public class VisitorsEntities1 : DbContext
{

public DbSet   Customer { get; set; }
public DbSet CustomerAddress { get; set; }
public DbSet Address { get; set; }
}

我的控制器

  public ActionResult Create()
    {
        VisitorsEntities1 db = new VisitorsEntities1();                    

 ViewBag.AreaID = new SelectList(db.Area, "AreaID", "DisplayName",   "PrintName");

   return View();
    }



    [HttpPost]
   public ActionResult Create(CustomerViewModel viewmodel)
    {

 ViewBag.AreaID = new SelectList(db.Area, "AreaID", "DisplayName",   viewmodel.AreaID);
       var Cusobj = Guid.NewGuid();
       var CusAddobj = Guid.NewGuid();
       var Addobj = Guid.NewGuid();


      var Customerobj = new Customer()
        {
            CustomerID = Cusobj,
        DisplayName = viewmodel.CustomerName,
        PrintName = viewmodel .CustomerName
   };


  var CustomerAddressobj = new CustomerAddress()
        {
            CustomerAddressID = CusAddobj,
            CustomerID = viewmodel.CustomerID,
            AddressID = viewmodel.AddressID
         };


      var Addressobj = new Address()
        {
            AddressID = Addobj,
            DisplayName= viewmodel.CustomerName,
            Street=viewmodel.Street,
            Location= viewmodel.Location,
            AreaID = viewmodel.AreaID,
            PinCode= viewmodel.PinCode
  };
     db.Customer.Add(Customerobj);
        db .CustomerAddress.Add(CustomerAddressobj );
        db.Address.Add(Addressobj);


   db.SaveChanges();


   return View()
 }

这里的AreaID是SubForm所以我使用ViewBag在View中输入下拉并传递Guid。现在我的问题是客户地址表。

 var CustomerAddressobj = new CustomerAddress()
        {
            CustomerAddressID = CusAddobj,
            CustomerID = viewmodel.CustomerID,
            AddressID = viewmodel.AddressID
         };

Here CustomerAddressID is new Guid is working fine. But

   CustomerID = viewmodel.CustomerID,
   AddressID = viewmodel.AddressID

这两行没有正确传递Guid。在表中这些列是00000-0000-0000-0000它没有显示任何错误,也没有正确传递GUID。我不知道问题是什么。请任何人帮忙解决这些问题?

错误

My Error

感谢。

1 个答案:

答案 0 :(得分:0)

尝试使用specual属性在插入时自动生成Guid键。

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public System.Guid CustomerID  { get; set; }

此处Key表示它是主键,DatabaseGenerated指示自动生成关键字段

修改

请注意,自动生成的标识字段仅在保存后分配给新实体:

using (var db = new Context())
{
    var customer = new Customer();

    // Here customer.CustomerID is empty     

    db.Customers.Add(customer);
    db.SaveChanges();

    // Here customer.CustomerID is assigned
}