针对deosnt的MVC下拉列表显示选定的值

时间:2015-07-02 15:54:01

标签: asp.net-mvc model-view-controller

我有下面的CSHTML页面,以便我能够编辑一个sugestions模型列表,我已经确定信息正被添加到每一行,但是当我将其更改为dropdown因为它似乎没有选择来自sugested bubject Id的任何想法的值,列表显示正常,但加载时没有选择任何信息。 任何想法为什么会这样。

        @model List<CISI.Mvc.Models.SuggestionModel> 
        @using Telerik.Sitefinity.UI.MVC
        @using Telerik.Sitefinity.UI.MVC.Helpers
        <div class="container">
            @using (Html.BeginFormSitefinity("edit", "edit"))
            {

                @Html.ValidationSummary(true)

                <fieldset>
                    <table style="border:4px;">
                        <tr>
                            <td>
                                Subject ID
                            </td>
                            <td>
                                Suggestion order
                            </td>
                            <td>
                                Suggested subject
                            </td>
                        </tr>
                        @for (int i = 0; i < Model.Count; i++)
                        {
                            <tr>
                                <td>@Html.TextBoxFor(m => m[i].subject_id, new { @readonly = "readonly" })
                                </td>
                                <td>@Html.TextBoxFor(m => m[i].suggestion_order)
                                </td>
                                <td>@Html.DropDownListFor(m => m[i].suggested_subject_id, Model[i].sugestedSubject)
                                </td>
                            </tr>
                        }
                    </table>
                    <input type="submit" value="confirm" runat="server" />
                </fieldset>
            }
        </div>

      public List<eLearningSubjectSuggestionModel> getSubjectSuggestionsEdit(int subjectID)
            {
               // moduleSuggsetionList list = new moduleSuggsetionList();
                List<SubjectSuggestionModel> list2 = (from c in db.subject_suggestion_tbl
                                                              where c.subject_id == subjectID && c.deleted == false
                                                              orderby c.suggestion_order
                                                              select new SubjectSuggestionModel
                                                              {
                                                                  subject_id = c.subject_id,
                                                                  suggestion_order = c.suggestion_order,
                                                                  suggested_subject_id = c.suggested_subject_id,
                                                                  //sugestedSubject = getsubjectIEnumLsistSuggestions()

                                                              }).ToList();
                foreach (var i in list2)
                {
                    i.sugestedSubject = getsubjectIEnumLsistSuggestions();
                }
                //list.suggest = list2;
                return list2;
            }
            public IEnumerable<SelectListItem> getsubjectIEnumLsistSuggestions()
            {
                IEnumerable<SelectListItem> selectList = from c in db.subjects_tbl
                                                         select new SelectListItem
                                                         {
                                                             Text = c.subject_title,
                                                             Value = SqlFunctions.StringConvert((double)c.subject_id).Trim()
                                                         };
                return selectList;
            }

 public class SubjectSuggestionModel
    {
        public int? subject_suggestion_id { get; set; }
        public int? subject_id { get; set; }
        public int? suggested_subject_id { get; set; }
        public IEnumerable<SelectListItem> sugestedSubject { get; set; }
        public string suggestedSubjectText { get; set; }
        public int? suggestion_order { get; set; }
    }

2 个答案:

答案 0 :(得分:1)

您的问题很可能是您未在控制器中选择Model[i].sugestedSubject中的其中一个选项。

我猜测Model[i].sugestedSubjectSelectList所以当您添加值时,请务必使用suggested_subject_id作为object selectedValue参数{ {1}}构造函数

SelectList功能中,您应添加getsubjectIEnumLsistSuggestions参数

subject_id

然后在获得列表时将其传递给

public IEnumerable<SelectListItem> getsubjectIEnumLsistSuggestions(double subject_id)

在您的函数中,使用此ID设置所选项目。

foreach (var i in list2)
{
   i.sugestedSubject = getsubjectIEnumLsistSuggestions(i.subject_id);
}

答案 1 :(得分:0)

sugestedSubject应为IEnumerable<SelectListItem>。其中一个ID应与suggest_subject_id匹配。