MVC 4 C#CRUD DropDownList选择未被选中

时间:2016-03-16 23:20:07

标签: c# asp.net-mvc-4 crud html.dropdownlistfor

我正在尝试创建从2个单独的表填充的2个DropDownLists,并填充第3个表User_Roles。我想在创建和编辑时执行此操作。我正在尝试为用户分配报告名称。这是我的模特:

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;
        }
    }
}

这是我的控制器:

   private REPORTS_DBEntities db = new REPORTS_DBEntities();
    public ActionResult Create()
    {            
        ViewBag.Report_Names = new SelectList(db.ZAPR_REPORT, "REPORT_ID", "REPORT_NAME");
        ViewBag.User_Names = new SelectList(db.ZAPR_USER, "USER_ID", "NAME");

        return View();
    }

    //
    // POST: /UserReports/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(ZAPR_USER_REPORTS zapr_user_reports)
    {
        if (ModelState.IsValid)
        {
            db.ZAPR_USER_REPORTS.Add(zapr_user_reports);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(zapr_user_reports);
    }

这是我的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")
}

我的下拉列表会在显示创建页面时显示,但是当选择了选项,然后单击“创建”按钮时,没有选择任何值....我只是在这两个字段中获得Null。我究竟做错了什么?谢谢!

0 个答案:

没有答案
相关问题