简体   繁体   English

如何在MVC中正确设置Dropdownlist Selected Value

[英]How to set Dropdownlist Selected Value correctly in MVC

I'm currently posting a List<MyModel> to a Controller in MVC4. 我目前正在向List<MyModel>中的Controller发布List<MyModel>

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. 我在视图中使用for循环,所以我可以将我的列表发布到控制器。

The ID for the textbox is [0].PurgeSeconds. 文本框的ID为[0] .PurgeSeconds。 When I replace it with a dropdownlist with the same ID it just doesn't work. 当我用具有相同ID的下拉列表替换它时,它就不起作用。

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>
}

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). YOUR_LIST是列表,您可以在下拉列表中显示项目(可能是MyMvc.Models.Configuration.DataRetentionConfig()。PurgeTimeOptions(),我不知道)。 It is better to use ViewModel, where you can store YOUR_LIST and another data to display and edit. 最好使用ViewModel,您可以在其中存储YOUR_LIST和其他数据以进行显示和编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM