我正在使用一种方法来生成我的问题和答案我的意思是每个问题都出现在视图中,这个问题的答案都会放到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;
}
在我在控制器中的create方法中,我将字符串传递给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'><option value='value1'>displaytext</option></select></span>
正如您在上面的代码中看到的那样id=1
和价值value1
以及displaytext
的问题是在httppost create controller
中生成的,我需要获取questionId
我的意思是1
,我的意思是value1
。我用Google搜索了一下,我找到了一些关于form collection
的内容但我不知道我该怎么做?
每个想法都将受到赞赏。
祝你好运
public ActionResult Create(FormCollection formCollection)
{
??????get value and question id to save
return RedirectToAction("Create", "Score");
}
Html创建视图:
@{
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" value="????? ???????" Class="buttonSave" />
</div>
}
答案 0 :(得分:0)
尝试使用此方法获取formcollection中的控件值
formCollection["1"]
Html代码: -
<span style='float:left'><select id='1' name="1"><option value='value1'>displaytext</option></select></span>
您的代码变为
[HttpPost]
public ActionResult Create(FormCollection formCollection)
{
String Val1 = formCollection["1"].value;
}
答案 1 :(得分:-1)
您可以使用此代码迭代所有formCollection项。
public ActionResult Create(FormCollection formCollection)
{
foreach (var key in formCollection.Keys)
{
var value = formCollection[key.ToString()];
// save value
}
return RedirectToAction("Create", "Score");
}