MVC 4 C#积垢下拉列表选择未被选中

MVC 4 C#积垢下拉列表选择未被选中,c#,asp.net-mvc-4,crud,html.dropdownlistfor,C#,Asp.net Mvc 4,Crud,Html.dropdownlistfor,我试图创建两个从两个单独的表填充的DropDownList,并填充第三个表用户角色。我希望在创建和编辑时执行此操作。我正在尝试将报表名称分配给用户。这是我的模型: namespace ReportMenu.Models { public class UserReports { [Key] public string user_id { get; set; } public string report_id { get; set; }

我试图创建两个从两个单独的表填充的DropDownList,并填充第三个表用户角色。我希望在创建和编辑时执行此操作。我正在尝试将报表名称分配给用户。这是我的模型:

namespace ReportMenu.Models
{
    public class UserReports
    {
        [Key]
        public string user_id { get; set; }
        public string report_id { get; set; }
        [Required]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
        [DataType(DataType.Date)]
        public DateTime valid_date { get; set; }
    }

    public class UsersReportsContext : DbContext
    {
        public DbSet<ZAPR_USER_REPORTS> UserReportsDetail
        {
            get;
            set;
        }
    }
}
以下是我的create.cshtml视图:

@model ReportMenu.ZAPR_USER_REPORTS

@{
    ViewBag.Title = "Create";
}

<script src="~/Scripts/datepicker.js"></script>

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>ZAPR_USER_REPORTS</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.USER_ID)
        </div>
        <div class="editor-field">
            @Html.DropDownList("User_Names", "Select a user")
            @Html.ValidationMessageFor(model => model.USER_ID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.REPORT_ID)
        </div>
        <div class="editor-field">
            @Html.DropDownList("Report_Names", "Select a report")
            @Html.ValidationMessageFor(model => model.REPORT_ID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.VALID_DATE)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.VALID_DATE, new { @class = "datetype", type = "text" })
            @Html.ValidationMessageFor(model => model.VALID_DATE)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
@model ReportMenu.ZAPR\u用户报告
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
ZAPR_用户_报告
@LabelFor(model=>model.USER\u ID)
@Html.DropDownList(“用户名”,“选择用户”)
@Html.ValidationMessageFor(model=>model.USER\u ID)
@LabelFor(model=>model.REPORT\u ID)
@Html.DropDownList(“报告名称”、“选择报告”)
@Html.ValidationMessageFor(model=>model.REPORT\u ID)
@LabelFor(model=>model.VALID\u日期)
@TextBoxFor(model=>model.VALID_DATE,新的{@class=“datetype”,type=“text”})
@Html.ValidationMessageFor(model=>model.validate\u日期)

} @ActionLink(“返回列表”、“索引”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }

当显示“创建”页面时,会显示我的下拉列表,但当选择并单击“创建”按钮时,不会选择任何值……我只会在这两个字段中得到空值。我做错了什么?谢谢

未绑定到模型中的属性。使用
@Html.DropDownListFor(m=>m.user\u id,(SelectList)ViewBag.user\u name,“选择一个用户”)
(如果在POST方法中返回视图,请确保重新分配
ViewBag
属性。但是,我强烈建议您停止使用
ViewBag
并使用强类型视图模型。您应该使用
DropDownListFor()
helper方法当您实际应该使用
@Html.DropdownList时,您似乎正在使用
@Html.DropdownListFor
。谢谢您,Stephen!!!这很有效!
@model ReportMenu.ZAPR_USER_REPORTS

@{
    ViewBag.Title = "Create";
}

<script src="~/Scripts/datepicker.js"></script>

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>ZAPR_USER_REPORTS</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.USER_ID)
        </div>
        <div class="editor-field">
            @Html.DropDownList("User_Names", "Select a user")
            @Html.ValidationMessageFor(model => model.USER_ID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.REPORT_ID)
        </div>
        <div class="editor-field">
            @Html.DropDownList("Report_Names", "Select a report")
            @Html.ValidationMessageFor(model => model.REPORT_ID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.VALID_DATE)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.VALID_DATE, new { @class = "datetype", type = "text" })
            @Html.ValidationMessageFor(model => model.VALID_DATE)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}