だから私はを使用してオブジェクトを更新しようとしています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.");
}
[..]
}
各プロパティを手動で更新する代わりに、 DropDownListFor
withを使用する簡単な方法はありますか?TryUpdateModel