2012-06-14 70 views
2

我試圖創建具有一對多關係的實體框架的ASP.NET MVC應用程序。爲此,我已成功設法將適當的項目列表加載到下拉控件(在「創建」視圖中),但是當我單擊「創建」按鈕(在「創建」視圖中)頁面驗證爲faild時,驗證錯誤消息爲The value '1' is invalid.ASP.NET MVC頁面驗證失敗(值無效)

錯誤

模型

public class Post 
{ 
    public int Id { get; set; } 
    ... 
    public virtual Person Author { get; set; } 
} 

DataBaseContext

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
    modelBuilder.Entity<Post>() 
       .HasOptional(p => p.Author); 
} 

控制器

public ActionResult Create() 
    { 
     PopulateAuthorDropDownList(); 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult Create(Post post) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Posts.Add(post); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     PopulateAuthorDropDownList(post.Author); 
     return View(post); 
    } 

    private void PopulateAuthorDropDownList(object selectedPerson = null) 
    { 
     var personQuery = from d in db.People 
          orderby d.Name 
          select d; 
     ViewBag.Author = new SelectList(personQuery, "Id", "Name", selectedPerson); 
    } 

查看

<div class="editor-label"> 
     @Html.LabelFor(model => model.Author) 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("Author", String.Empty) 
     @Html.ValidationMessageFor(model => model.Author) 
    </div> 

當我在數據庫中查詢作者表有記錄ID爲1,所以我猜1是一個有效的值。我在這裏錯過了什麼?

在此先感謝...

+0

你在哪裏得到這個錯誤?這是一個例外嗎?它是無效的ModelState?你的模型類是否有DataAnnotation屬性? – Jan

+0

@Jan Page.IsValid失敗,消息顯示在視圖「ValidationMessageFor」字段中。 – Nalaka526

回答

1

管理人

改變模式

public class Post 
{ 
    public int Id { get; set; } 
    ... 
    public int Author { get; set; } // changed type to int 
} 

改變查看

<div class="editor-label"> 
     @Html.LabelFor(model => model.Author) 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("AuthorId", String.Empty) 
     @Html.ValidationMessageFor(model => model.Author) 
    </div> 

改變控制器

01來解決這個問題
private void PopulateAuthorDropDownList(object selectedPerson = null) 
    { 
     var personQuery = from d in db.People 
          orderby d.Name 
          select d; 
     ViewBag.AuthorId = new SelectList(personQuery, "Id", "UserName", selectedPerson); //Changed Author to AuthorId 
    } 

DataBaseContext

總之謝謝你的答案刪除

modelBuilder.Entity<Post>() 
       .HasOptional(p => p.Author); 

... :)

1

你應該有這樣的事情:

<div class="editor-label"> 
    @Html.LabelFor(model => model.Author.Name) 
</div> 
<div class="editor-field"> 
    @Html.DropDownListFor(model => model.Author.Name, (SelectList)ViewBag.AuthorList) 
    @Html.ValidationMessageFor(model => model.Author.Name) 
</div> 

請注意,您應該命名ViewBag.AuthorList而不是ViewBag.Author

+0

感謝您的回答,但顯示相同的錯誤消息 – Nalaka526

+0

嘗試從下拉列表中刪除String.Empty – karaxuna

+0

並使用dropdownlistfor。請參閱更新 – karaxuna

2

你沒有表現出如何Author物體看起來像,

假設如果是這樣,

public class Author 
{ 
    public int Id{get;set;} 
    public string Name{get;set;} 
} 

試試這個,

<div class="editor-label"> 
    @Html.LabelFor(model => model.Author) 
</div> 
<div class="editor-field"> 
    @Html.DropDownListFor(model => model.Author.Id, ViewBag.Author, "Select an Author") 
    @Html.ValidationMessageFor(model => model.Author) 
</div> 
0

你必須提供的選項列表DropDownList,你只是過客string.empty

和我你應該使用強類型版本DropDownListFor

@Html.DropDownListFor(model => model.Author.Name, Model.AuthorList) 

,或者甚至更好的通過不使用名稱,但編號:

@Html.DropDownListFor(model => model.Author.Id, Model.AuthorList) 

而且你的模型:

class Model { 
    ... 
    public SelectList AuthorList {get;set;} 
} 

並在您的控制器中:

model.AuthorList = new SelectList(fetchAuthors() 
         .Select(a => new SelectListItem { 
              Text = a.Name, 
              Value = a.Id 
             }, "Value", "Text");