在mvc中获取标准DDL列表的值

本文关键字:列表 DDL 标准 mvc 获取 | 更新日期: 2023-09-27 18:21:06

我正在使用一种生成问题和答案的方法,我的意思是每个问题都会出现在视图中,并且这个问题的答案会被放入DDL中,如您所见:

public string GenerateHtmlCode()
        {
            string result = string.Empty;
            List<ExecutiveOfficer> executiveOfficers = executiveOfficerRepository.GetAll().ToList();
            List<Indicator> indicators = indicatorRepository.GetAll().ToList();
            foreach (ExecutiveOfficer executiveOfficer in executiveOfficers)
            {
                result += "<div style='width:100%;float:right;'><span>" + executiveOfficer.ExecutiveOfficerText +
                         "</span><span style='float:left'>" +
                         GenerateAnswer(indicatorRepository.FindBy(i => i.ExecutiveOfficerId == executiveOfficer.Id
                                                                  ).ToList(), executiveOfficer.Id) + "</span></div>";
            }

            return result;
        }

在我在控制器中的创建方法中,我将字符串传递给viewbag,如您所见:

  [HttpGet]
        public ActionResult Create(int preliminaryId)
        {
            ViewBag.preliminaryId = preliminaryId;
            ViewBag.pageGenarator = objScoreRepository.GenerateHtmlCode();
           // List<ExecutiveOfficer> executiveOfficer = objScoreRepository.GetAll().ToList();
            //ViewBag.executiveOfficerName = new SelectList(executiveOfficer, "Id", "ExecutiveOfficerText");
            return View("Create");
        }

我的html代码:

   </span><span style='float:left'><select id='1' name ='1'><option value='value1'>displaytext</option><option value='value2'>displaytext2</option></select></span>

正如你在这里看到的,我有1个DDL,它有2个值,我需要获得用户选择的值,我使用form collection来获得DDL的名称,如你在这里所见:

[HttpPost]
        //[Authorize(Roles = "Expert")]
        public ActionResult Create(FormCollection formCollection)
        {
            //objScoreRepository.Add(Score);
            //objScoreRepository.Save();
            foreach (var VARIABLE in formCollection.AllKeys)
            {

            }
            return RedirectToAction("Create", "Score");
        }

formcollection只返回一条记录,该记录为1,即DDL的名称。但我需要得到DDL的值。我该怎么做?

我的观点:

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_LayoutIdeaOtherPage.cshtml";
}
    <h2>Create</h2>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        @Html.Raw(ViewBag.pageGenarator)
        <div class="buttonPossion">
                    <input type="submit"  Class="buttonSave" />
                </div>
    }

致以最良好的问候。

在mvc中获取标准DDL列表的值

您可以使用

foreach(string item in form.AllKeys)
{
  var value = Request[item];
}