开发者

Passing form data back to a controller

开发者 https://www.devze.com 2023-02-27 11:36 出处:网络
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 Li

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" id=@option.Level>
        @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开发者_如何学Python 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.


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)


Try replacing :

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

with:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号