2016-10-02 29 views
1

我想創建一個簡單的網站,用戶可以添加一些細節,如標題,描述,城市,時間等。實體框架一對多關係代碼第一個asp.netnet mvc 5和jquery自動完成

這是我的數據庫圖的一部分。

enter image description here

  1. 用戶可以創建多個offerts
  2. 城市可以有很多offerts
  3. 地區可以有許多城市。

市臺

public class City 
{ 
    public int CityID { get; set; } 

    [StringLength(50, MinimumLength = 2)] 
    public string Name { get; set; } 

    public int RegionID { get; set; } 
    public virtual Region Region { get; set; } 

    public virtual ICollection<Offert> Offerts { get; set; }   
} 

Region表

public class Region 
{ 
    public int RegionID { get; set; } 

    [StringLength(30, MinimumLength=3)] 
    public string Name { get; set; } 

    public virtual ICollection<City> Cities { get; set; } 
} 

Offert表:

public class Offert 
{ 
    // Properties 
    public int OffertID { get; set; } 

    [Required] 
    [MaxLength(30)] 
    [Display(Name = "Title", ResourceType = typeof(Resource))] 
    public string Title { get; set; } 

    [Required] 
    [DataType(DataType.MultilineText)] 
    [Display(Name = "Description", ResourceType = typeof(Resource))] 
    [StringLength(100, ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "Bad_Description", MinimumLength = 6)] 
    public string Description { get; set; } 

    [Required] 
    [DataType(DataType.DateTime)] 
    [Display(Name = "When", ResourceType = typeof(Resource))] 
    public DateTime When { get; set; } 

    // Relationships 
    public int CityID { get; set; } 
    [Display(Name = "City", ResourceType = typeof(Resource))] 
    public virtual City City { get; set; 

    public virtual ApplicationUser User { get; set; } 
} 

用戶表:

public class ApplicationUser : IdentityUser 
{  
    public virtual ICollection<Offert> Offerts { get; set; } 
    public virtual MyUserInfo MyUserInfo { get; set; } 

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
    { 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
     return userIdentity; 
    } 
} 

關係是使用流利的API創建:

public class Pako_Context : IdentityDbContext<ApplicationUser> 
{ 
    public Pako_Context() 
     : base("database_context") 
    { 
     this.Configuration.LazyLoadingEnabled = false; 
     this.Configuration.ProxyCreationEnabled = false; 
    } 

    public static Pako_Context Create() 
    { 
     return new Pako_Context(); 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 

     modelBuilder.Entity<ApplicationUser>() 
      .HasMany(user => user.Offerts) 
      .WithRequired(); 

     modelBuilder.Entity<Region>() 
      .HasMany(c => c.Cities) 
      .WithRequired(r => r.Region); 

     modelBuilder.Entity<City>() 
      .HasMany(o => o.Offerts) 
      .WithRequired(c => c.City); 
    } 

    public DbSet<City> Cities { get; set; } 
    public DbSet<Region> Regions { get; set; } 
    public DbSet<Offert> Offerts { get; set; } 
    public DbSet<MyUserInfo> MyUserInfo { get; set; } 
} 

我有2個問題:

  1. 如何通過從視圖中的數據到控制器? 當前jquery自動完成機制返回包含城市名稱的字符串,但此值在控制器內不可見。

Offert創建視圖:

<div class="form-group"> 
     @Html.LabelFor(model => model.City.Name, "City", htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.City.Name, new { htmlAttributes = new { @class = "form-control", @id = "autocomplete", data_autocomplete_url = Url.Action("Autocomplete") } }) 

      @Html.ValidationMessageFor(model => model.City.Name, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

2.目前的問題數1解決方法,我只是從數據庫中選取一些隨機的城市,加入到我的offert。

Offert控制器創建方法:

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include = "OffertID,Title,Description,When,City.Name")] Offert offert) 
    { 
     if (ModelState.IsValid) 
     { 
      string currentUserId = User.Identity.GetUserId(); 
      ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId); 
      offert.User = currentUser; 
      City RandomCity = db.Cities.FirstOrDefault(); 
      offert.City = RandomCity; 
      db.Offerts.Add(offert); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     return View(offert); 
    } 

但是,當我調用 db.SaveChanges();以下例外 方法發生:

附加信息:實體在「Pako_Context.Offerts」參加「ApplicationUser_Offerts」的關係。 0相關'ApplicationUser_Offerts_Source'被找到。 1'ApplicationUser_Offerts_Source'是預期的。

我試圖添加offert記錄到ApplicationUser,但它沒有奏效。可能我沒有正確理解這個信息。 如何解決問題1和2?

回答

0

對於您的第一個問題,您必須更改自動完成並通過JavaScript創建,以便從用戶中選擇所選值,然後在隱藏字段中更新此值。 你必須要與名稱這個隱藏字段CityID

 @Html.HiddenFor(model => model.CityID) 

如果使用的是自動完成的插件,那麼就應該是這樣的:

$(「#CITY_NAME」)自動完成({ 源: 「在這裏把源爲您的城市」, 的minLength:2, 選擇:函數(事件,UI){ $( 「#CityID」)VAL(ui.item.id); 。}} )

至於seco nd問題,它說你有一個用戶和優惠之間的關係,並且這種關係是必需的,所以你必須在優惠系列中至少有一個優惠,所以嘗試添加這一行:

currentUser.Offerts.Add(offert); 
相關問題