2011-02-14 118 views
0

我搜索並找到以下鏈接link alink b,這些鏈接沒有解釋我做錯了什麼。控制器將參考實例保存爲新實例,而不是更新現有參考實例

我使用MVC3 +流利+ NHibernate的用流利的automapper模式具有以下配置:

NHibernate.Cfg.Configuration().Configure(); 
XXConfiguration autoMappingConf = new XXConfiguration(); 
FluentConfiguration fc = Fluently.Configure() 
    .Database(MsSqlConfiguration.MsSql2008.ConnectionString(
     ConfigurationManager.ConnectionStrings[myConnString].ConnectionString 
    ) 
     .ShowSql()); 
fc.ExposeConfiguration(cfg =>  cfg.SetProperty(NHibernate.Cfg.Environment.CurrentSessionContextClass, "web")); 
fc.Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<BasicEntity>(autoMappingConf) 
       .UseOverridesFromAssemblyOf<AccountMappingOverride>() 
       .Conventions.Add(DefaultCascade.All())) 
       ); 
     return fc.BuildSessionFactory(); 
    } 

,並與以下重寫:

public class AccountMappingOverride: IAutoMappingOverride<Account> 
{ 
    public void Override(AutoMapping<Account> mapping) 
    { 
     mapping.HasMany(a => a.ChildrenAccounts).KeyColumn("ParentAccount_id"); 
    } 
} 

我有以下型號:

public abstract class Entity<TId> 
{ 
    public virtual TId Id { get; set; } 
    public virtual int ExternalId { get; set; } 
} 

public abstract class BasicEntity : Entity<int>{} 

public abstract class MainEntity : BasicEntity 
{ 
    public virtual int Version { get; set; } 
    public virtual bool Active { get; set; } 
} 

public class Language : MainEntity 
{ 
    public virtual string Code { get; set; } 
    public virtual string Name { get; set; } 
} 

public class Account : MainEntity 
{ 
    public virtual string Name { get; set; } 
    public virtual Language Language { get; set; } 
    public virtual Account ParentAccount { get; set; } 
    public virtual IList<Account> ChildrenAccounts { get; set; } 
    public virtual IList<User> Users { get; set; } 

}

public class User : MainEntity 
{ 
    public virtual string Name { get; set; } 
    public virtual string Surname { get; set; } 
    public virtual string Email { get; set; } 
    [DataType(DataType.Password)] 
    public virtual string Password { get; private set; } 
    public virtual Language Language { get; set; } 
    public virtual Account Account { get; set; } 
    public virtual DateTime? LastLoginDate { get; set; } 
    public virtual DateTime? LastPasswordChangeDate { get; set; } 
} 

和顯示領域中創建一個新帳戶視圖:

@model XX.Models.Account 

@{ 
    ViewBag.Title = "New Account"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>New account</h2> 

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Name) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Name) 
     @Html.ValidationMessageFor(model => model.Name) 
    </div> 


    <div class="editor-label"> 
     @Html.LabelFor(model => model.ParentAccount) 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownListFor(model => model.ParentAccount.Id, XX.Utils.SelectList<XX.Models.Account>("Name", (string)ViewBag.parentAccountId)) 
     @Html.ValidationMessageFor(model => model.ParentAccount) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Language) 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownListFor(model => model.Language.Id, XX.Utils.SelectList<XX.Models.Language>("Name", "")) 
     @Html.ValidationMessageFor(model => model.Language) 
    </div> 

    <p> 
     <input type="submit" value="Add" /> 
    </p> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

控制器保存,但不是在保存過程新的語言被創建,而不是更新的簡單分配領域的語言,現有的。檢查傳遞給控制器​​的Account對象,我們注意到它有一個對已經存在的id的Language對象的引用。這是爲任何參考映射完成的。

[HttpPost] 
[NeedsPersistence] 
public ActionResult Create(Account account) 
    { 
     if (ModelState.IsValid) 
     { 
      accountDao.Save(account, currentUser); 
      return RedirectToAction("Tree"); 
     } 
     else { 
      return View(); 
     } 
    } 

如何獲得保存以識別語言已存在?

回答

0

看到這一行:

@ Html.DropDownListFor(型號=> model.Language.Id,XX.Utils.SelectList( 「姓名」, 「」))

你告訴什麼顯示字段是但數據字段是空字符串。

我想這會是這樣的:

@ Html.DropDownListFor(型號=> model.Language.Id,XX.Utils.SelectList( 「名稱」, 「ID」))

+0

剛纔忘了粘貼XX的代碼。 Utils.SelectList – user616397 2011-03-01 08:35:47

0

我們通過定義的ViewModels解決的問題(我們保留了語言屬性,因爲它是從模型下降,但適用於這種情況ParentAccount)和控制器如下:

public class AccountEdit 
{ 
    public int Id { get; set; } 
    [Required] 
    [LocalizedDisplayName("Name")] 
    public string Name { get; set; } 
    [LocalizedDisplayName("ParentAccount")] 
    public int ParentAccount { get; set; } 
    [LocalizedDisplayName("Role")] 
    public Role Role { get; set; } 

    public AccountEdit() { } 

    public AccountEdit(Account a) 
    { 
     Id = a.Id; 
     Name = a.Name; 
     ParentAccount = a.ParentAccount!=null ? a.ParentAccount.Id : 0; 
     Role = a.Role; 
    } 

    public Account BoundAccount() 
    { 
     DataAccessObject<Account> dao = new DataAccessObject<Account>(); 
     Account account = dao.Get(Id); 
     account.Name = Name; 
     if (ParentAccount == default(int)) 
      account.ParentAccount = null; 
     else 
      account.ParentAccount = dao.Get(ParentAccount); 
     return account; 
    } 
} 

    [HttpPost] 
    [NeedsPersistence] 
    public ActionResult CreateDistributor(DistributorCreate dc) 
    { 
     if (ModelState.IsValid) 
     { 
      Account account = dc.BoundAccount(); 
      accountDao.Save(account, currentUser); 
      return RedirectToAction("Index"); 
     } 
     else { 
      return View(dc); 
     } 
    }