大家好,我在这方面真的很新!
我已经被困在这里很长时间了。请我真的需要你的帮助。谢谢!
我的问题是。如何将我的所有图像显示到 MVC 中的选择列表或下拉列表中,然后将其发布到网站上?我有一个带有 PicID、PicTitle 等的数据库。
我想显示或显示该文件夹中的图像,然后可以选择一张图片,然后将其显示在视图中。
在我的创建视图中,我有:
<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);
}
请帮助我并感谢所有人。