I am retrieving some configuration from a service and passing this data to the edit view. The user can make changes to the text and check box fields and these values are returned fine.
I have a drop down list populated with the values from an enum for the database type. These values are passed to the view and are displayed correctly but when the user changes the selection, the first value in the list is applies to the data model.
Below is some code from the controller and view.
The Controller:
[HttpPost]
public ActionResult Edit( SettingsModel config)
{
try
{
List<string> configErrors = null;
if (ModelState.IsValid)
{
// Set up a channel factory to use the webHTTPBinding
using (WebChannelFactory<IChangeService> serviceChannel = new WebChannelFactory<IChangeService>(new Uri(baseServiceUrl)))
{
IChangeService channel = serviceChannel.CreateChannel();
configErrors = channel.SetSysConfig(config);
// Check for any errors returned by the service
if (configErrors != null || configErrors.Count > 0)
{
// Display the errors at the top of the page
ViewData["ConfigErrors"] = configErrors;
// TODO: Force the redisplay of the page
}
}
}
ViewData["DBTypes"] = new SelectList(Enum.GetValues(typeof(DatabaseType)), config.DBType);
return RedirectToAction("Index", config);
}
catch
{
return View();
}
}
The Edit view:
<tr>
<td>
<h4>Database Type</h4>
</td>
<td>
@Html.DropDownList("Database Type", ViewData["DBTypes"] as SelectList )
</td>
<td>
@Html.ValidationMessageFor(model => model.DBType)
</td>
</tr>
Try to change the view like this
This should persist the selection to the property
DBTypeof your modelOr change your row like this to obtain the same result