【发布时间】: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