Asp.net mvc 提交表单时,dropdownlist没有值

Asp.net mvc 提交表单时,dropdownlist没有值,asp.net-mvc,Asp.net Mvc,我尝试在视图中使用下拉列表来显示作者(用户)列表。我能够填充此下拉列表,并在我的视图中查看内容。提交表单时,我在控制器中调试操作,检查模型时,与下拉列表关联的字段值为空 这是我的动作控制器(在显示我的视图之前): public ActionResult Create() { IEnumerable authors=m_AccountBusiness.GetAllUsers(); PageCreateViewModel viewModel=新的PageCreateViewModel { PageT

我尝试在视图中使用下拉列表来显示作者(用户)列表。我能够填充此下拉列表,并在我的视图中查看内容。提交表单时,我在控制器中调试操作,检查模型时,与下拉列表关联的字段值为空

这是我的动作控制器(在显示我的视图之前):

public ActionResult Create()
{
IEnumerable authors=m_AccountBusiness.GetAllUsers();
PageCreateViewModel viewModel=新的PageCreateViewModel
{
PageToCreate=新建PageFullViewModel(),
Authors=Authors.Select(x=>newselectListItem{Text=x.UserName,Value=x.UserID.ToString()})
};
返回视图(viewModel);
}
以下是我的观点(部分):

@model MyBlog.ViewModels.PageCreateViewModel
创造
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.HiddenFor(model=>model.PageToCreate.PageID)
@LabelFor(model=>model.PageToCreate.Title)
@Html.TextBoxFor(model=>model.PageToCreate.Title,新的{@class=“titleValue”})
@Html.ValidationMessageFor(model=>model.PageToCreate.Title)
@LabelFor(model=>model.PageToCreate.Author)
@Html.DropDownListFor(model=>model.PageToCreate.Author,model.Authors,“请选择…”)
以下是我的PageCreateViewModel:

public class PageCreateViewModel
{
    public PageFullViewModel PageToCreate { get; set; }
    public IEnumerable<SelectListItem> Authors { get; set; }
}
公共类PageCreateViewModel
{
公共页面FullViewModel页面创建{get;set;}
公共IEnumerable作者{get;set;}
}
有什么想法吗


谢谢。

您必须向您的PageCreateViewModel添加一个额外的字符串属性。在此属性中,我们将存储选定的值。假设它的名称为“Author”。编辑:我注意到您的模型中有一个属性,但请尝试这样做

dropdownlist填充在视图中需要如下所示

 @Html.DropDownList("Author", Model.Authors)

谢谢你们。我终于发现了我的错误:它不是Author绑定到的正确属性,它必须是authord

<div class="editor-field">           
@Html.DropDownListFor(model => model.PageToCreate.AuthorID, Model.Authors, "Please select...")         
</div> 

@Html.DropDownListFor(model=>model.PageToCreate.authord,model.Authors,“请选择…”)

无论如何,谢谢。

谢谢您的回复,但它不起作用。我的控制器中仍然没有下拉列表的值。@Bronzato和您模型中的其他值在返回时正确填写了?哈哈……你知道吗,这是我的第一个想法,但后来我认为这是理所当然的事情,您必须进行交叉检查……无论如何祝你好运
 @Html.DropDownList("Author", Model.Authors)
<div class="editor-field">           
@Html.DropDownListFor(model => model.PageToCreate.AuthorID, Model.Authors, "Please select...")         
</div>