2015-05-11 48 views
0

我已經使用所有正確的值填充了DropDownListFor。我傳遞了一個Guid參數,但不知道如何將默認值設置爲該Guid。填充DropDownListFor並且默認值是guid參數

查看

<div class="form-group"> 
    @Html.LabelFor(model => model.Parent) 
    @Html.DropDownListFor(model => model.Parent, new SelectList(Model.AllParents, "Id", "Name"), string.Empty, new { @class = "form-control" }) 
    @Html.ValidationMessageFor(model => model.Parent) 
</div> 

視圖模型

public class ParentViewModel 
{ 
    public List<Parent> AllParents { get; set; } 

    [DisplayName("Parent")] 
    public Guid? ParentId { get; set; } 

} 

控制器

[HttpGet] 
public ActionResult Create(Guid? id) 
{ 

      var parentViewModel = new ParentViewModel 
      { 
       AllParents = _Service.GetAll().ToList() 
      }; 
      return View(parentViewModel); 
     } 
    } 
+1

你需要 「做你想做默認值」'@ Html.DropDownListFor(型號=> model.Parent,新的SelectList(Model.AllParents更換'string.Empty', 「Id」,「Name」),「默認值在這裏」,新的{@class =「form-control」})' –

+0

geez ...完全忽略了這一點。以答案的形式複製和粘貼,以便我可以給你信用。謝謝! – JoshYates1980

回答

2

您需要更換的String.Empty 「默認值做你想做」

@Html.DropDownListFor(model => model.Parent, new SelectList(Model.AllParents, "Id", "Name"), "Default value is here here", new { @class = "form-control" })