我有類崗位:獲取用戶名從用戶ID在MVC3
public class Post
{
[Key]
public int PostID { get; set; }
[DisplayName("Text komentáře")]
[Required(ErrorMessage = "Je potřeba vyplnit text komentáře")]
public string Text { get; set; }
public DateTime PostDate { get; set; }
public int TopicID { get; set; }
public int UserID { get; set; }
public virtual User User { get; set; }
}
和用戶等級:
public class User
{
public int UserID { get; set; }
public string UserName { get; set; }
public FullName Name { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public IIdentity Identity{ get; set; }
public virtual Role Right { get; set; }
public virtual ICollection<Post> Posts { get; set; }
public virtual ICollection<Topic> Topics { get; set; }
}
主題類:
public class Topic
{
public int TopicID { get; set; }
[Required(ErrorMessage = "Je potřeba vyplnit titulek")]
[DisplayName("Titulek")]
public string Title { get; set; }
[Required(ErrorMessage = "Je potřeba vyplnit obsah příspěvku")]
[DataType(DataType.MultilineText)]
[DisplayName("Obsah článku")]
public string Text { get; set; }
public int UserID { get; set; }
public virtual User User { get; set; }
// tento atribut nám nastaví jméno atribut, které bude na stránce
[DisplayName("Datum zveřejnění")]
public DateTime PublishedDate { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
在控制器我有顯示這個方法話題:
public ActionResult Zobrazit(int id)
{
var topic = db.Topics.Find(id);
var username = db.Users.Find(topic.UserID).UserName;
ViewBag.UserName = username;
return View(topic);
}
和查看Zobrazit看起來是這樣的:
<div class="topicHeader">@Html.DisplayFor(model => model.Title)</div>
<fieldset>
<p>Autor: @Html.ActionLink((string)ViewBag.UserName, "Podrobnosti", "Uzivatel", new { id = Model.UserID }, null)
Zasláno: @Html.DisplayFor(model => model.PublishedDate)</p>
<p>@Html.DisplayFor(model => model.Text)</p>
</fieldset>
@if (Model.Posts != null)
{
foreach (SkMoravanSvitavka.Models.Post comment in Model.Posts)
{
@Html.Partial("_Post", comment)
}
}
@if (Context.User.Identity.IsAuthenticated)
{
using (Html.BeginForm("Vytvorit", "Post"))
{
<div id="posts">
<input type="hidden" name="TopicID" value="@Model.TopicID" />
@*<label for="Author">Email:</label> <input type="text" name="Author"/> *@
<label for="Text">
Komentář:</label>
<br />
<textarea name="Text" rows="5" cols="40"></textarea>
<br />
<input type="submit" value="Přidat komentář" />
</div>
}
}
,並最終爲後部分觀點如下:
@model SkMoravanSvitavka.Models.Post
<fieldset>
<p>
Author: @Html.ActionLink((string)ViewBag.UserName, "Podrobnosti", "Uzivatel", new { id = Model.AuthorID }, null) Přidáno: @String.Format("{0:g}", Model.PostDate)
</p>
<p>
@Model.Text
</p>
</fieldset>
我的問題是,我在數據庫中每一個職位保存的用戶ID和我可以在控制器的主題(讓我說我有論壇),並在ViewBag中「發送」它,但我不知道如何獲取用戶名在部分視圖中從用戶名發佈。感謝您的幫助
編輯: 我做了這是在Eranga後的變化(增加公共虛擬用戶的用戶,添加模型構建器,...),但現在我有重新創建數據庫的問題。在VS出現錯誤:
The database creation succeeded, but the creation of the database objects did not. See inner exception for more details.
然後YSOD:
System.Data.SqlServerCe.SqlCeException: The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = Topic_User ]
EDIT2: 我改變類的帖子,話題,用戶無論是在原來的職位,我添加的showen,此處數據庫方面:
public class TeamContext : DbContext
{
public DbSet<Player> Players { get; set; }
public DbSet<Team> Teams { get; set; }
public DbSet<New> News { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<Topic> Topics { get; set; }
protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
modelBuilder.Entity<Post>().HasRequired(p => p.User)
.WithMany(u => u.Posts)
.HasForeignKey(p => p.UserID);
modelBuilder.Entity<Topic>().HasRequired(t => t.User)
.WithMany(u => u.Topics)
.HasForeignKey(t => t.UserID);
}
}
您能否給我一些示例代碼!大聲笑;) – 2012-03-11 13:03:34