填充DropDownListFor,默认值为guid参数

时间:2015-05-11 15:11:00

标签: drop-down-menu asp.net-mvc-5 guid html.dropdownlistfor

我已经使用所有正确的值填充了DropDownListFor。我正在传递Guid参数,但不知道如何将默认值设置为该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 个答案:

答案 0 :(得分:2)

你需要用&#34替换string.Empty;你想要的默认值&#34;

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