0

だから私はを使用してオブジェクトを更新しようとしていますTryUpDateModel、問題はドロップダウンにあります。

プロパティとして画像オブジェクトを持つオブジェクトがあります。

SelectListItems現在、objectsクラスをモデルとして使用しており、objectsプロパティに設定する画像のGUIDに値が設定されているリストからドロップダウンを生成しています。

私のビューコード:

@Html.LabelFor(m => m.RibbonImage, "Ribbon Image:")
@Html.DropDownListFor(m => m.RibbonImage, (List<SelectListItem>)ViewBag.RibbonImages)

リストを生成する場所:

ViewBag.RibbonImages = this.Datastore.GetAll<Image>()
                                    .Where(x => x.Type == ImageType.Ribbon)
                                    .Select(x => new SelectListItem()
                                    {
                                        Text = x.Description + " \u2013 " + Path.GetFileName(x.Filename),
                                        Value = x.Id.ToString()
                                    })
                                    .ToList();

メインオブジェクトクラスの私のプロパティ:

/// <summary>
/// Gets or sets the ribbon image to use
/// </summary>
public virtual Image RibbonImage { get; set; }

私の行動方法:

[HttpPost]
[..]
public ActionResult Update(Guid? id)
{
    RibbonLookup ribbon = this.Datastore.Query<RibbonLookup>().SingleOrDefault(x => x.Id == id);

    [..]

    string[] properties = new string[]
    {
        "RibbonImage"
    };

    if (this.TryUpdateModel<RibbonLookup>(ribbon, properties))
    {
        this.Datastore.Update<RibbonLookup>(ribbon);

        ModelState.AddModelError(string.Empty, "The ribbon has been updated.");
    }
    else
    {
        ModelState.AddModelError(string.Empty, "The ribbon could not be updated.");
    }

    [..]
}

各プロパティを手動で更新する代わりに、 DropDownListForwithを使用する簡単な方法はありますか?TryUpdateModel

4

1 に答える 1

0

あなたの質問を完全に理解していませんでした。ただし、最初に、ドロップダウンリストコントロールをどのように使用するかを共有します。以下のコードで質問に答えられない場合は、問題の説明について詳しく知りたいと思います。

これは通常、アプリケーションでドロップダウンリストを使用する方法です。フォームの投稿では、SelectedCustomerIdを含む親モデルが投稿された(選択された)SelectedCustomerIdから構築されます。デフォルトのモデルバインダーは問題なくバインドします。

意見 :

@model SampleDropDown.Models.CustomerData

using (Html.BeginForm("Continue", "Customer"))
{
    if (Model.Customers != null)
    {
@Html.DropDownListFor(m => m.SelectedCustomerId, new SelectList(Model.Customers, "CustomerId", "DisplayText"))

    }

<input type="submit" value="Select" />
}


public class CustomerData
   {

    public List<Customer> Customers { get; set; }
    public string SelectedCustomerId { get; set; }
    public string Response { get; set; }
    }

 public class Customer
    {

    public string DisplayText { get; set; }
    public string CustomerId { get; set; }


   }

コントローラー:

  [HttpPost]
    public ActionResult Continue(CustomerData data)
    {
        return View("Index", data);
    }
于 2012-08-27T20:39:06.493 に答える