我使用的代码与我程序中其他地方的代码非常相似。它在那里工作。为什么,当我点击提交时,我在下拉列表中选择的项目是否未绑定到模型?
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);
}
}
我没有包含_APIResults部分视图,因为buildstate始终为0,当它出现时应该以-1或4进入(假设您将下拉列表更改为Halt或Ready,这就是我所做的。