我有一个控制器,如下所示
[HttpPost]
public ActionResult LoginResult()
{
//want get the data here
return View();
}
我有一个视图,我使用下面给出的模型渲染视图
@model IEnumerable<My.Models.SurveyData>
@{
ViewBag.Title = "Index";
}
<h2>Survey Form</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
@using (Html.BeginForm("LoginResult", "My"))
{
foreach (var item in Model)
{
<tr>
<h4> @Html.DisplayFor(modelItem => item.Question)</h4>
<br />
@if ((int)item.InputType == 1)
{
<input type="text" id="@item.Id" name="@item.Id" value="" />
}
else if ((int)item.InputType == 2)
{
<textarea rows="4" cols="50" id="@item.Id" name="@item.Id"></textarea>
}
else if (item.InputType.ToString().Trim().ToLower() == "checkbox")
{
for (int i = 0; i < @item.NumberOfChoice; i++)
{
<input type=@item.InputType.ToString().Trim().ToLower() name="@item.AdminAnswers.ElementAt(i).Answer"></input>@item.AdminAnswers.ElementAt(i).Answer
<br />
}
}
}
<input type="submit" value="Login" />
}
现在我希望将表单提交中的所有输入字段值放入LoginResult方法中,该方法在Controller中命名为&#34; My&#34;,但所有输入字段名称将动态生成。那我怎么能在方法中得到这个值呢。
答案 0 :(得分:0)
请改用列表,并将foreach
循环替换为for
循环:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@for (var i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.HiddenFor(x => x[i].IP)
@Html.CheckBoxFor(x => x[i].Checked)// input your model item
</td>
<td>
@Html.DisplayFor(x => x[i].IP)
</td>
</tr>
}
<div>
<input type="submit" value="Submit" />
</div>
}
在您的控制器操作
中[HttpPost]
public ActionResult LoginResult(IEnumerable<SurveyData> surveyData)
{
}
答案 1 :(得分:0)
You just have to put model in your Post method like this:
[HttpPost]
public ActionResult LoginResult(IEnumerable<My.Models.SurveyData> model)
{
//Here you can access your model data
//want get the data here
return View();
}
Also do following change in your View :
@using (Html.BeginForm("LoginResult", "My", FormMethod.Post))
{
// Your current code
}