【问题标题】:Get the value of standard DDL list in mvc获取mvc中标准DDL列表的值
【发布时间】:2014-09-09 06:10:18
【问题描述】:

我正在使用一种方法来生成我的问题和答案,我的意思是每个问题都出现在视图中,并且这个问题的答案被放入 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 个具有 2 个值的 DDL 我需要获取用户选择的值我使用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>
    }

最好的问候。

【问题讨论】:

  • 在我看来..只需重新设计您的页面架构,您将在控制器中附加 html,这不是一个好主意..!!!
  • @Kartikeya 那么您还有其他想法吗?

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


【解决方案1】:

你可以使用

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

【讨论】:

  • 不,如果您的 html 是正确的并且您在下拉列表中选择了第一个选项,它将返回“value1”。但是你为什么不使用模型和 html 助手来呈现你的视图并利用 MVC 的绑定特性呢?
  • 我有一个包含问题的表格和一个包含答案的表格,这两个表格之间存在一对多的关系,所以我需要显示问题和答案(进入 ddl),我不不知道我该怎么做?!!!
  • 您没有在您的 GenerateHtmlCode 方法中生成任何输入,因此除了您显示的 &lt;select&gt; 之外,其他任何内容都不会发回。您在控制器中创建 html 的代码很糟糕。您确实需要遵循一些有关使用 MVC 的基本教程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-19
  • 1970-01-01
  • 1970-01-01
  • 2011-04-20
  • 1970-01-01
相关资源
最近更新 更多