0

I've used code very similar to what follows elsewhere in my program. It works there. Why, when I click submit, does my selected item in the dropdownlistfor NOT bind to the model?

public class APIDataViewModel
    { 
        public ChangeBuildState ChangeBuildStateValue { get; set; }
        public int SelectedIntValue { get; set; }
    }

 public class ChangeBuildState
    {
        public Dictionary<int, string> BuildState { get; set; }

        public ChangeBuildState()
        {
            BuildState = new Dictionary<int, string>()
            {
                { -1, "Halt"},
                { 4, "Ready"}
            };

        }
    }

@using (Ajax.BeginForm(
                            "ChangeBuildStateRun",
                            "Stores",
                            new
                            {
                                Area = "",
                                locationNumber = Model.locationNumber,
                                macAddress = Model.mac,
                                buildState = Model.SelectedIntValue
                            },
                            new AjaxOptions
                            {
                                UpdateTargetId = "APIModal",
                                InsertionMode = InsertionMode.Replace,
                                HttpMethod = "POST",
                                OnSuccess = "CloseAddWkstnModals",
                                OnFailure = "APIFailed"
                            },
                            new
                            {
                                id = "ChangeBuildStateForm"
                            }))

                {
                    <div class="row Padding_Std center-block">
                        <div class="col-sm-7 col-sm-offset-4 col-md-7 col-md-offset-2 Padding_Std">
                            <label class="ModalInputLblStyle">IP Address: </label>
                            @Html.EditorFor(m => m.ipaddress,
                             new
                             {
                                 htmlAttributes = new
                                 {
                                     @class = "input-sm AddEditWorkstationTBoxStyle",
                                     @readonly = "readonly",
                                     @disabled = "disabled"
                                 }
                             })
                        </div>
                    </div>
                    <div class="row Padding_Std center-block">
                        <div class="col-sm-7 col-sm-offset-4 col-md-7 col-md-offset-2 Padding_Std">
                            <label class="ModalInputLblStyle">MAC Address: </label>
                                @Html.EditorFor(m => m.mac,
                                new
                                {
                                    htmlAttributes = new
                                    {
                                        @class = "input-sm AddEditWorkstationTBoxStyle",
                                        @readonly = "readonly",
                                        @disabled = "disabled"
                                    }
                                })
                        </div>
                    </div>
                    <div class="row Padding_Std center-block">
                        <div class="col-sm-7 col-sm-offset-4 col-md-7 col-md-offset-2 Padding_Std">
                            <label class="storeSearchInputLblStyle">Select Build State: </label>
                                @Html.DropDownListFor(m => m.SelectedIntValue,
                                    new SelectList(
                                    Model.ChangeBuildStateValue.BuildState,
                                    "Key",
                                    "Value"),
                                    "--- Select ---",
                                    new { @class = "DDL_LogLevelStyle" })
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-9 col-sm-offset-4">
                            <div class="col-md-4 col-sm-3 center-block" id="BtnChangeBuildState">
                                <input type="submit" value="Change Build State" class="btn btn-sm button-alt-1 button-v center-block" />
                            </div>
 ...
                    </div>
                }

public ActionResult ChangeBuildStateRun(int locationNumber = 0, string macAddress = "", int buildState = 999)
        {
            APIDataViewModel model = new APIDataViewModel();
            model.locationNumber = locationNumber.ToString();
            model.mac = macAddress;
            try
            {
                bool apiCallResult = _epmService.UpdateStoreWorkstationBuildState(locationNumber, macAddress, buildState);
                model.webapi_name = "Change Build State";
                model.response_type = "ChangeBuild";
                model.success = apiCallResult;
                return PartialView("_APIResults", model);
            }
            catch
            {
                model.webapi_name = "Change Build State";
                model.response_type = "ChangeBuild";
                model.success = false;
                return PartialView("_APIResults", model);
            }
        }

I haven't included the _APIResults partial view, as buildstate always comes in as 0, when it appears it should come in as -1 or 4 (assuming you change the dropdown to Halt or Ready, which is what I did.

JohnC.IT
  • 33
  • 5
  • It is unclear what your asking here, or what your issue is. Is that method you have shown the POST method for your form? And your not generating a form control for anything named `buildState` (your dropdownlist is for a property named `SelectedIntValue` so it will bind to a parameter name `SelectedIntValue`). All you do is set a route value for `buildState` to the initial value of `SelectedIntValue` which is `0` because that id the default value for an `int` –  Jan 23 '18 at 23:45
  • Yes, that method POST is for the form, though I've tried GET as well. buildState is just the variable name I am calling it for the ChangeBuildState Controller. SelectedIntValue is in the model, and it's supposed to bind to that model. I tried changing the buildState variable name to SelectedIntValue, but that still winds up passing 0 instead of -1 or 4. – JohnC.IT Jan 24 '18 at 00:17
  • Oh, I see why you are confused. It cut off my question at the top. I've edited it so my question is complete – JohnC.IT Jan 24 '18 at 00:19
  • If you add a parameter `int SelectedIntValue` in you POST method, its value will be bound to the value of the selected option. And what do you mean, _not bind to the model_ - your POST method does not even have a model as its parameter –  Jan 24 '18 at 00:21
  • The Ajax.BeginForm uses APIDataViewModel at the top. The way I'm using it here has always worked before. locationNumber and macAddress in the same section ARE being set properly. Is there something wrong with the way I've defined ChangeBuildState? – JohnC.IT Jan 24 '18 at 00:27
  • I do not understand what you mean - my comment was that you NOT binding to your model in the POST method - just to some parameters (i.e you do not have a parameter which is `APIDataViewModel model`) –  Jan 24 '18 at 00:32
  • And _is there something wrong_ - your code is an awful way to generate a dropdownlist. Use a view model, and that view model will contain an `IEnumerable` property for the options - refer the code in [this Q/A](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) for a typical example –  Jan 24 '18 at 00:34
  • I think I have an idea where the problem is. I have some code elsewhere that is similar to this, but I discovered it's not passing the value to the model correctly, either ...DropDownListFor(m => new SelectList( Model.LogLevelValue.L_Level, "Key", "Value"), "--- Select ---", new { @class = "DDL_LogLevelStyle" }) – JohnC.IT Jan 24 '18 at 00:38
  • I must not be using the Dictionary correctly. Have some more research to do. – JohnC.IT Jan 24 '18 at 00:39
  • The `DropDownListFor()` code in your previous comment does not even compile! And you are using the `Dictionary` correctly, albeit its a terrible way to generate a dropdownlist. Again, your posting back a name/value pair in the request for `SelectedIntValue` so you POST method needs a parameter with the same name (or a model containing a property with that name) –  Jan 24 '18 at 00:43

0 Answers0