1

通常、DropDownListFor ヘルパーを使用するときは、ID (int) に基づいて選択されている項目を選択していますが、ID ではなくテキスト (文字列) に基づいて選択されている項目を表示する必要がある状況になりました。コントローラーでは、モデルはこのプロパティで必要な値に正しく設定されています:

model.Title

タイトルの例は「Front Office」です。コントローラーに次のコードがあります。

ViewBag.Titles = new SelectList(jobTitles, "Name", "Name");

ビューで私はこれを持っています:

@Html.DropDownListFor(model => model.Title, ViewBag.Titles as SelectList)

DropDownList には、予想されるすべての役職が正しく入力されていますが、 model.Titleに基づいて正しい役職が選択されていません。私は何を間違っていますか?

アップデート:

私のコードには他にも何か問題がある可能性があるようです。

コントローラ:

public ActionResult Edit(int id = 0)
    {
        StaffMember staffmember = StaffMember.SelectByID(id); // gets staff member from db

        ViewBag.Titles = new SelectList(JobTitle.SelectAll(), "Name", "Name", staffmember.Title); // JobTitle.SelectAll() returns List<JobTitle>

        StaffEditModel model = new StaffEditModel();
        model.ID = staffmember.ID;
        model.ClientID = staffmember.ClientID;
        model.FirstName = staffmember.FirstName;
        model.MiddleInitial = staffmember.MiddleInitial;
        model.LastName = staffmember.LastName;
        model.Title = staffmember.Title;
        model.Phone = staffmember.Phone;
        model.Email = staffmember.Email;
        model.Birthday = staffmember.Birthday;
        model.HireDate = staffmember.HireDate;
        model.Biography = staffmember.Biography;

        if (staffmember == null)
        {
            return HttpNotFound();
        }
        return View(model);
    }

モデル:

public class StaffEditModel
{
    public int ID { get; set; }

    public int ClientID { get; set; }

    [Required]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Display(Name = "Middle Initial")]
    public string MiddleInitial { get; set; }

    [Required]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    public string Title { get; set; } // this is the property I'm trying to show in DropDown

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public string Birthday { get; set; }

    [Display(Name = "Hire Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public string HireDate { get; set; }

    public string Email { get; set; }

    [MaxLength(14)]
    public string Phone { get; set; }
    public string Biography { get; set; }
}

意見:

@Html.DropDownListFor(model => model.Title, new SelectList(ViewBag.Titles,"Value","Text", Model.Title))
4

2 に答える 2

3

これはうまくいくはずです。コントローラーからViewBag.Titlesそのまま渡すことができますList<SelectListItem>

var jobList = new List<SelectListItem>();
foreach (var job in jobTitles)
{
    var item = new SelectListItem();
    item.Value = job.PropertyName; //the property you want to display i.e. Title
    item.Text = job.PropertyName;
    jobList.Add(item);
}
ViewBag.Title = jobList;

そしてビューで、

@Html.DropDownListFor(model => model.Title, new SelectList(ViewBag.Titles,"Value","Text", Model.Title))
于 2013-06-22T15:07:55.663 に答える
1

私はこれについてパーティーに遅れていることを知っていますが、同様の問題があり、見つけたものを追加すると思いました.

理由はまだわかりません-まだ調査中です(それが私がここにたどり着いた方法です)-しかし、タイトルという言葉を使用しているという事実と関係があると思います

まったく同じシナリオがあり、モデルをTitleではなくCustomerTitleに変更するとすぐに、期待どおりに機能しました。

最初に考えたのは、モデル バインダーがDOM の別の場所でTitleを見つけて、つまずいたということです。完全な憶測。もっと時間があれば、理由を掘り続けます

于 2013-10-21T10:25:51.427 に答える