1

を使用して、edit.cshtml ファイルのドロップダウン リストに医療製品のブランド名のリストを入れようとしています。Html ヘルパーを適切に使用しようとしていますが、うまくいきません。Model.BrandSelectListItem は、DropDownList に入ることになっている IEnumerable です。 Html.DropDownListFor()

また、モデルをラムダ式では「モデル」として参照する必要があるのに、次のコード セグメントの 2 番目の引数では「モデル」として参照する必要がある理由がわかりません。

@Html.DropDownListFor(model => model.BrandName, Model.BrandSelectListItem)

コードを実行すると、次の実行時エラーが発生します。

キー 'BrandName' を持つ ViewData アイテムのタイプは 'System.String' ですが、タイプは 'IEnumerable' である必要があります。

このエラーに関連する可能性があるいくつかのクラスを次に示します。

医療製品

public class MedicalProduct
{
    [Key]
    public int ID { get; set; }

    [Required]
    [StringLength(50)]
    public string Name { get; set; }

    [Required]
    [DataType(DataType.Currency)]
    public double Price { get; set; }

    // is a foreign key
    public Nullable<int> BrandID { get; set; }
}

MedicalProductViewModel

public class MedicalProductViewModel
{
    [Key]
    public int ID { get; set; }

    [Required]
    [StringLength(50)]
    public string Name { get; set; }

    [Required]
    [DataType(DataType.Currency)]
    public double Price { get; set; }

    public Nullable<int> BrandID { get; set; }

    [DisplayFormat(NullDisplayText="[Generic]")]
    public string BrandName { get; set; }

    public IEnumerable<SelectListItem> BrandSelectListItem { get; set; }
}

MedicalProductController

public class MedicalProductController : Controller
{
    private MvcMedicalStoreDb _db = new MvcMedicalStoreDb();

    //
    // GET: /MedicalSupply/Edit/5

    public ActionResult Edit(int id = 0)
    {
        MedicalProduct medicalProduct = _db.Products.Find(id);
        if (medicalProduct == null)
        {
            return HttpNotFound();
        }

        var viewModel = GetMedicalProductViewModel(medicalProduct);
        return View(viewModel);
    }

    public MedicalProductViewModel GetMedicalProductViewModel(MedicalProduct product)
    {
        var mapper = new MedicalProductMapper(_db);

        return mapper.GetMedicalProductViewModel(product);            
    }
}

MedicalProductMapper

public class MedicalProductMapper
{
    private MvcMedicalStoreDb _db;

    public MedicalProductMapper(MvcMedicalStoreDb db)
    {
        _db = db;
    }

    public MedicalProductViewModel GetMedicalProductViewModel(MedicalProduct source)
    {
        MedicalProductViewModel viewModel = new MedicalProductViewModel();
        var dbBrands = _db.Brands.ToArray();

        viewModel.ID = source.ID; 
        viewModel.Name = source.Name;
        viewModel.Price = source.Price;
        viewModel.BrandID = source.BrandID;
        if (viewModel.BrandID != null)
            viewModel.BrandName = dbBrands.SingleOrDefault(b => b.ID == source.BrandID).Name;

        var queryBrands = from b in dbBrands
                          select b;

        // BrandSelectListItem is 'null' before this assignment statement executes. 
        // and Stays null after the statement executes, why?
        viewModel.BrandSelectListItem = queryBrands as IEnumerable<SelectListItem>;

        return viewModel;
    }
}

Edit.cshtml

@model MvcMedicalStore.Models.MedicalProductViewModel

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>MedicalProduct</legend>

        @Html.HiddenFor(model => model.ID)

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Price)
            @Html.ValidationMessageFor(model => model.Price)
        </div>


        <div class="editor-label">
            @Html.LabelFor(model => model.BrandName)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.BrandName, Model.BrandSelectListItem)
            @Html.ValidationMessageFor(model => model.BrandName)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
4

1 に答える 1