2016-08-28 39 views
0

不知道我在做什麼錯,但我的下拉列表不想選擇我希望它選擇的值。我有以下DropDownListFor不選擇視圖模型字段的值

控制器動作

// GET: /Contract/Create 
public ActionResult Create() 
{ 
    var model = new ContractViewModel(); 

    var authors = _authorService.GetAuthors(); 
    var publishers = _publisherService.GetPublishers(); 

    model.AuthorsList = new SelectList(authors, "AuthorID", "Name", authors.First()); 
    model.PublishersList = new SelectList(publishers, "PublisherID", "Name", publishers.First()); 

    return View(model); 
} 

// POST: /Contract/Create 
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(ContractViewModel contractViewModel) 
{ 
    if (ModelState.IsValid) 
    { 
     Contract contract = new Contract(); 
     contract.CanUsePublisherPartners = contractViewModel.CanUsePublisherPartners; 

     contract.Author.AuthorID = Convert.ToInt32(contractViewModel.SelectedAuthorID); 
     contract.Publisher.PublisherID = Convert.ToInt32(contractViewModel.SelectedPublisherID); 


     var success = _contractService.AddContract(contract); 
     if (success) 
     { 
      return RedirectToAction("Index"); 
     } 
    } 

    contractViewModel.AuthorsList = new SelectList(_authorService.GetAuthors(), "AuthorID", "Name"); 
    contractViewModel.PublishersList = new SelectList(_publisherService.GetPublishers(), "PublisherID", "Name"); 

    ViewBag.ErrorMessage = "An error occured when trying to add the Contract. A contract between this Author and Publisher may already exist! Please try again and if the problem persists, contact the Sys Admin."; 
    return View(contractViewModel); 
} 

視圖模型

public class ContractViewModel 
{ 
    [Display(Name = "Can the author distribute through the publisher's partners?")] 
    public bool CanUsePublisherPartners { get; set; } 
    [Display(Name="Author")] 
    public int? SelectedAuthorID { get; set; } 
    [Display(Name = "Publisher")] 
    public int? SelectedPublisherID { get; set; } 

    public SelectList AuthorsList { get; set; } 
    public SelectList PublishersList { get; set; } 

} 

查看下拉列表的結合

<div class="form-group"> 
    @Html.LabelFor(model => model.SelectedAuthorID, new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.HiddenFor(m => m.SelectedAuthorID) 
     @Html.DropDownListFor(m => m.SelectedAuthorID, Model.AuthorsList) 
    </div> 
</div> 
<div class="form-group"> 
    @Html.LabelFor(model => model.SelectedPublisherID, new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.HiddenFor(m => m.SelectedPublisherID) 
     @Html.DropDownListFor(m => m.SelectedPublisherID, Model.PublishersList) 
    </div> 
</div> 

什麼問題? 當我提交我的形式,SelectedAuthorIDSelectedPublisherID的值是INT默認 - 0

我真的在束手無策這裏,我看了一些細節,如果試圖找出他們影響任何東西例如。當Selected容器屬性與列表項的值屬性等名稱相同時,有些人遇到了麻煩。

如果有人有任何建議將是偉大的分享他們!

回答

0

我相信問題是您的頁面上有SelectedPublisherIDSelectedAuthorID兩次。

Html.HiddenFor(m => m.SelectedAuthorID)不應該是必要的,除了DropDownListFor

一個小問題,一般C#命名約定使用PascalCase,這意味着屬性應該被命名爲SelectedAuthorId而不是ID

+0

感謝您花時間看我的問題,您剛剛幫我解決了它!我也很感激命名約定的建議,我會重構:)乾杯! –

相關問題