Alex
Alex

Reputation: 1567

How to set Dropdownlist Selected Value correctly in MVC

I'm currently posting a List<MyModel> to a Controller in MVC4.

I can correctly update all of the models I post, however when the page loads I cannot select the correct value in the dropdownlist.

This works correctly if I use a textbox, however the dropdownlist won't pick the correct value. The list is correctly populated when the page loads.

I'm using a for loop in the View so I can post my list to the controller.

The ID for the textbox is [0].PurgeSeconds. When I replace it with a dropdownlist with the same ID it just doesn't work.

How do I set the dropdownlist to be the correct selected value?

Model

namespace MyMvc.Models
{
    [Table("MyTable")]
    public class DataRetentionConfig
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Name { get; set; }
        public int PurgeSeconds { get; set; }
    }
}

Controller

[HttpPost]
public ActionResult Save(ICollection<DataRetentionConfig> models)
{
    foreach (var model in models)
    {
        using (var db = new FoySqlContext())
        {
            var retentionRecord = db.DataRetentionConfigs.Find(model.Id);
            retentionRecord.PurgeTime = model.PurgeTime;
            db.SaveChanges();
        }
    }

    return RedirectToAction("Index");
}

View

@model List<MyMvc.Models.DataRetentionConfig>
@{
    ViewBag.Title = "Index";
}
@using (Html.BeginForm("Save", "Retention", FormMethod.Post))
{
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Name</th>
                <th>Purge Seconds</th>
            </tr>
        </thead>
        <tbody>
            @for (var i = 0; i < Model.Count; i++)
            {
                if (Model[i].DataType == 1)
                {
                    <tr>
                        <td>
                            @Html.HiddenFor(m => m[i].Id)
                            @Model[i].Name
                        </td>
                        <td>
                            @*@Html.TextBoxFor(m => m[i].PurgeSeconds)*@
                            @Html.DropDownListFor(m => m.[i].PurgeSeconds, new MyMvc.Models.Configuration.DataRetentionConfig().PurgeTimeOptions(), new { @name = Model[i].PurgeTime, @class = "form-control", @style = "width: 150px;" })
                        </td>
                    </tr>
                }
            }
        </tbody>
    </table>
    <p>
        <input type="submit" value="Save" class="btn btn-large btn-primary" />
        @*<button type="reset" class="btn">Cancel</button>*@
        <a class="btn" href="@Url.Action("Index", "Retention")">Cancel</a>
    </p>
}

Upvotes: 0

Views: 870

Answers (1)

Krahu
Krahu

Reputation: 183

Try

@Html.DropDownListFor(m => m.[i].PurgeSeconds, new SelectList(YOUR_LIST, "PurgeSeconds", "Name", m.[i].PurgeSeconds), new { @name = Model[i].PurgeTime, @class = "form-control", @style = "width: 150px;" })

YOUR_LIST is the list, where you have items to show in dropdown (probably MyMvc.Models.Configuration.DataRetentionConfig().PurgeTimeOptions(), I do not know). It is better to use ViewModel, where you can store YOUR_LIST and another data to display and edit.

Upvotes: 2

Related Questions