1

こんにちは皆さん、これは本当に新しいです!

私は長い間ここで立ち往生しています。本当にあなたの助けが必要です。ありがとう!

あなたへの私の質問です。すべての画像を MVC の選択リストまたはドロップダウン リストに表示して、サイトに投稿するにはどうすればよいですか? PicID、PicTitle などのデータベースを取得しました。

そのフォルダの画像を表示または表示してから、画像を選択してビューに表示できるようにしたいと考えています。

私のCreate Viewには次のものがあります:

<h2>Create</h2>

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

<fieldset>
    <legend>Picture</legend>

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

    <div class="editor-label">
        @Html.LabelFor(model => model.PicUrl)
    </div>
    <div class="editor-field">
        @Html.Action(Model.PicID)
        @Html.ValidationMessageFor(model => model.PicID)
    </div>

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

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

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

    <div class="editor-label">
        @Html.LabelFor(model => model.CatID, "Category")
    </div>
    <div class="editor-field">
        @Html.DropDownList("CatID", String.Empty)
        @Html.ValidationMessageFor(model => model.CatID)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

そして私のコントローラーで:

        public ActionResult Create()
    {
        ViewBag.CatID = new SelectList(db.Categories, "CatID", "CatName");
        return View();
    }

    //
    // POST: /Picture/Create



    [HttpPost]
    public ActionResult Create(Picture picture, HttpPostedFileBase file)
    {
        if (file != null && file.ContentLength > 0)
        {
            var FileName = string.Format("{0}.{1}", Guid.NewGuid(), file.ContentType);
            var path = Path.Combine(Server.MapPath("~/Images_upload"), FileName);
            file.SaveAs(path);
        }
        if (ModelState.IsValid)
        {
            db.Pictures.AddObject(picture);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.CategoryID = new SelectList(db.Pictures, "PicID", "PicTitle", picture.PicID);
        return View(picture);
    }

私を助けてください、そしてすべてに感謝します。

4

1 に答える 1