将表单数据传递回控制器

发布于 2024-11-02 07:27:59 字数 1211 浏览 1 评论 0原文

我遇到了参数无法从表单数据获取值的问题。它显示列表中正确的项目数(即,如果用户选择 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴梦长久 2024-11-09 07:27:59

您的视图代码有点令人困惑,但据我了解,您希望 ModelBinder 在发布时将单选按钮值绑定到 OptionIds 列表。在这种情况下,单选按钮的名称应为 OptionIds[0]OptionIds[1] 等。同样,我不确定 Level 是什么 属性是,但我假设你想要这样的东西:

@Html.RadioButton("OptionIds["+(option.Level-1)+"]", option.OptionId)

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 be OptionIds[0], OptionIds[1], etc. So again, I am not sure what the Level property is, but I assume you want something like this:

@Html.RadioButton("OptionIds["+(option.Level-1)+"]", option.OptionId)
樱花落人离去 2024-11-09 07:27:59

尝试将 : 替换

  @Html.RadioButton("["+(option.Level-1)+"].OptionId", option.OptionId)

为:

  @Html.RadioButton("["+(option.Level-1)+"]", option.OptionId)

Try replacing :

  @Html.RadioButton("["+(option.Level-1)+"].OptionId", option.OptionId)

with:

  @Html.RadioButton("["+(option.Level-1)+"]", option.OptionId)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文