html下拉列表到控制器

时间:2016-10-28 12:35:34

标签: c# html asp.net-mvc

我试图通过帖子将下拉列表的选定值传递给我的控制器,但我一直得到一个空值

***** HTML *****

p><b>Employee: &nbsp;&nbsp;&nbsp;&nbsp; @Html.DropDownList("EmpList", new SelectList((IEnumerable) @ViewData["EmployeeList"], "EmployeeName", "EmployeeName"))

控制器

[HttpPost]
    public ActionResult Save()
    {
        var employeeName = Request.Form["EmpList"];
        var manager = Session["LoggedUserFullName"]; 
    }

2 个答案:

答案 0 :(得分:0)

为什么不试试ViewModel和SelectListItem?我会做这样的事情

<强>控制器:

Model.Employees = employeesList.Select(employee => new SelectListItem
{
    Text = employee.FullName,
    Value = employee.Id
}).ToList();

<强> HTML:

@Html.DropDownListFor(m => m.EmployeeId, new SelectList(Model.Employees, "value", "text"))

EmployeeId 应为 string 。然后你只需要设置EmployeeId。

答案 1 :(得分:-1)

public class VM_News : M_News
{
    public SelectList CategoriesList { get; set; }

    [Display(Name = "Categories")]
    public List<int> CategoriesListSelected { get; set; }
}


    public IActionResult Create()
    {
        VM_News vm_News = new VM_News();

        var categoriesList =
            _context.
            db_Category.
            OrderBy(z => z.Title).
            Select(x => new { Id = x.IDCategory, Value = x.Title });

        vm_News.DateCreated = DateTime.Now;
        vm_News.IsActive = true;
        vm_News.CategoriesList = new SelectList(categoriesList, "Id", "Value");

        return View(vm_News);
    }


    public async Task<IActionResult> Create([Bind("IDNews,DateCreated,IsActive,Text,Title,CategoriesListSelected,OriginTitle,OriginUrl")] VM_News vm_News)
    {
        if (ModelState.IsValid)
        {
            M_News m_News = new M_News();
            m_News.Title = vm_News.Title;
            m_News.Text = vm_News.Text;
            m_News.IsActive = vm_News.IsActive;
            m_News.DateCreated = vm_News.DateCreated;
            m_News.OriginTitle = vm_News.OriginTitle;
            m_News.OriginUrl = vm_News.OriginUrl;

            foreach (var item in vm_News.CategoriesListSelected)
            {
                M_CategoryNews MTM = new M_CategoryNews();
                MTM.M_News = m_News;

                M_Category category = _context.db_Category.SingleOrDefault(z => z.IDCategory == item);
                MTM.M_Category = category;

                _context.Add(MTM);
            }

            await _context.SaveChangesAsync();

            return RedirectToAction("Index");
        }

        var categoriesList =
                        _context.
                        db_Category.
                        OrderBy(z => z.Title).
                        Select(x => new { Id = x.IDCategory, Value = x.Title });

        vm_News.CategoriesList = new SelectList(categoriesList, "Id", "Value");

        return View(vm_News);
    }


    <form asp-action="Create">
        <div class="form-horizontal">
            <h4>M_News</h4>
            <hr />
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>

            <div class="form-group">
                <label asp-for="CategoriesListSelected" class="col-md-1 control-   label"></label>
                <div class="col-md-11">
                    <select asp-for="CategoriesListSelected" asp-items="@Model.CategoriesList" ></select>
                    <span asp-validation-for="CategoriesListSelected" class="text-danger" />
                </div>
            </div>