2016-11-18 43 views
1

我创建了一个视图,它创建了一个模型。型号有枚举领域,像这样:.net核心传递枚举查看

public ProjectType ProjectType { get; set; } 
public enum ProjectType : int 
{ 
    type1= 1, 
    type2 = 2, 
    type3 = 3, 
    other = 4 
} 

,并考虑,我展示它是这样的:

<div class="form-group"> 
    @Html.LabelFor(m => m.ProjectType, new { @class = "col-md-2 control-label" }) 
    <div class="col-md-10"> 
     @Html.DropDownListFor(m => m.ProjectType,Model.ProjectType, new { @class = "form-control" }) 
     @Html.ValidationMessageFor(m => m.ProjectType) 
    </div> 
</div> 

我,'M试图达到这个观点,我得到一个错误:

There is no argument given that corresponds to the required formal parameter 'htmlAttributes' of 'IHtmlHelper<Project>.DropDownListFor<TResult>(Expression<Func<Project, TResult>>, IEnumerable<SelectListItem>, string, object)'

我试图解决这个问题,通过创建HTML的帮手,但我需要使用System.Web.Mvc,但我可以”塔德成.NET的核心项目 是否有任何其他的解决方案呢?

回答

1

我认为你不能直接将枚举绑定到下拉列表。

尝试添加一个帮助器构造,如List<SelectListItem>,您可以绑定下拉列表。

public class ProjectViewModel 
{ 
    public ProjectViewModel() 
    { 
     ProjectTypes = new List<SelectListItem>(); 
    } 

    public string SelectedProject { get; set; } 
    public List<SelectListItem> ProjectTypes { get; set; } 
} 

您可以在视图中使用此类似:

@model ProjectViewModel 

<select asp-for="SelectedProject" asp-items="@(Model.ProjectTypes)"> 
    <option>Please select one</option> 
</select> 

这应该导致这样的结果:

DropDownList

Enum类保持不变

public enum ProjectType : int 
{ 
    type1 = 1, 
    type2 = 2, 
    type3 = 3, 
    other = 4 
}