将表单数据传递回控制器
我遇到了参数无法从表单数据获取值的问题。它显示列表中正确的项目数(即,如果用户选择 5 个选项,则列表包含 5 个项目),但所有值均为零。下面是我的 HTML 视图:
@using (Html.BeginForm())
{
@Html.HiddenFor(s => s.SOWId)
foreach (LabelTable.Domain.Entities.Option option in ViewBag.Options)
{
<div class="wizard-section" [email protected]>
@Html.RadioButton("["+(option.Level-1)+"].OptionId", option.OptionId) @option.OptionName
</div>
}
<div class="buttons">
<input type="submit", value="Continue", class="button"/>
</div>
}
这是我的控制器方法:
[HttpPost]
public ViewResult Wizard(StatementOfWork SOW, List<int> OptionIds)
{
//do something
}
OptionIds 在发布时包含以下内容: [0] = 0 [1] = 0 [2] = 0 等等...
我想做的是创建一个表单,向用户提供一些可供选择的选项(此表单是向导的一部分)。
有 5 级(或更多)选项。表单的所有数据都通过 ViewBag.Options 发送到视图。除第 1 级外,所有级别均被隐藏。在第 1 级上进行选择后,下一个级别将显示,依此类推。该表单仅回发通过每个级别选择的选项。最初,我是通过多次回发到服务器来执行此操作的,但我不喜欢这样(多次往返),
我计划将每个级别中选择的选项添加到我在向导的视图之间传递的 SOW 模型。
I am running into an issue with a parameter not getting the value from the form data. It is showing the correct number of items (i.e. if the user select 5 options, the list contains 5 items) in the List but all values are zero. Below is my from my HTML view:
@using (Html.BeginForm())
{
@Html.HiddenFor(s => s.SOWId)
foreach (LabelTable.Domain.Entities.Option option in ViewBag.Options)
{
<div class="wizard-section" [email protected]>
@Html.RadioButton("["+(option.Level-1)+"].OptionId", option.OptionId) @option.OptionName
</div>
}
<div class="buttons">
<input type="submit", value="Continue", class="button"/>
</div>
}
Here is my controller method:
[HttpPost]
public ViewResult Wizard(StatementOfWork SOW, List<int> OptionIds)
{
//do something
}
OptionIds contains the following upon posting:
[0] = 0
[1] = 0
[2] = 0
and so on...
What I am trying to do is create a form where the user is presented with some options to select from (this form is one section of a wizard).
There are 5 level (or more) of options. All data for the form is sent to the view via the ViewBag.Options. All levels are hidden except level 1. Upon making a selection on level 1 the next level shows and so on. The form only posts back the options selected via each level. Originally I was doing this with mulitple post backs to the server but I did not like that (to many round trips)
I plan to add the options selected in each level to the SOW model which I am passing from view to view of the wizard.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的视图代码有点令人困惑,但据我了解,您希望 ModelBinder 在发布时将单选按钮值绑定到 OptionIds 列表。在这种情况下,单选按钮的名称应为
OptionIds[0]
、OptionIds[1]
等。同样,我不确定Level 是什么
属性是,但我假设你想要这样的东西:Your View code is a bit confusing, but as far as I understand, you want the ModelBinder to bind your radiobutton values to the
OptionIds
list upon posting. In that case, the names of your radiobuttons should beOptionIds[0]
,OptionIds[1]
, etc. So again, I am not sure what theLevel
property is, but I assume you want something like this:尝试将 : 替换
为:
Try replacing :
with: