我有一个非常简单的下拉列表,它应该返回一个外键,以便我可以将上传的文件与一个类别相关联。
我正在使用MVC-5和Entity Framework。这些查询实际上都在一个存储库中,所以不要因此而激怒我。
注意:可以在下拉列表中看到类别。当我试图发回数据时,我收到了一些错误。
首先,我将向您展示我尝试实施Dropdownlist的两种方式,然后我将向您展示我的模型,视图和控制器代码
我使用Viewbag创建了一个SelectList
,我将其用于下拉菜单,如下所示:
控制器:
public class HomeController : Controller
{
private db Context;
public HomeController() { db = new Context(); }
[HttpGet]
public ActionResult HelloWorld()
{
var query = db.Table.Where(u => u.Random == "helloworld").ToList();
ViewBag.DropDown = new SelectList (query, "RandomID", "Random");
return View();
}
[HttpPost]
public ActionResult HelloWorld(IEnumerable<HttpPostedFileBase> file, Table table)
{
//Do Stuff
}
}
查看:
@using(Html.BeginForm())
{
@Html.AntiForgeryToken()
<input type="file" value="Select Images" multiple />
@Html.Label("Categories")
@Html.DropdownList("DropDown", null, "Select")
<input type="submit" value="Upload">
}
收到错误: 没有“IEnumerable”类型的ViewData项具有“RandomID”键。 ( On DropdownList )
该查询选择了Table
中Random = "helloworld"
的所有内容,并将其列入了一个列表,因此RandomID
应该在那里
我使用模型绑定并发送SelectList
并试图在Post Action中使用该模型
控制器:
public class HomeController: Controller
{
private db Context;
HomeController() { db = new Context(); }
[HttpGet]
public ActionResult HelloWorld(Model model)
{
dropdown = db.Table.ToList();
model.List = new SelectList(dropdown, "RandomID", "Random")
return View(model)
}
[HttPost]
public ActionResult HelloWorld(IEnumerable<HttpPostedFileBase> file, Model model)
{
//Do Stuff
}
}
查看:
除
外,View与上述完全相同@Html.DropDownListFor(u => u.RandomID, (SelectList)Model.List, "Select")
当然
@model Project.Models.Model
型号:
public class Model
{
public IEnumerable<SelectListItem> List { get; set; }
public int RandomID { get; set; }
}
收到错误: System.NullReferenceException:未将对象引用设置为对象的实例。 ( On DropdownListFor )
很清楚,ID是空的并且正在返回null
但是对于我的生活,我无法理解为什么。
控制器:
public ActionResult EditPhotos(AddImageModel model)
{
if (Session["id"] == null)
{
return RedirectToAction("Index");
}
else
{
var pics = query.GalleryCategories();
model.List = new SelectList(pics, "CategoryName", "CategoryName");
return View(model);
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditPhotos(IEnumerable<HttpPostedFileBase> files, AddImageModel model)
{
string[] Extensions = new string[] { ".jpg", ".Jpg", ".jpeg", ".png" };
try
{
if (files != null)
{
foreach (HttpPostedFileBase file in files)
{
var name = Path.GetFileName(file.FileName);
var ID = model.ID;
var ext = Path.GetExtension(file.FileName);
if (Extensions.Contains(ext))
{
var filepath = Path.Combine(Server.MapPath("~/Images/Gallery"), name);
query.AddPictures(filepath, ID);
file.SaveAs(filepath);
return View();
}
else
{
ViewBag.Message = "Accepted file types are: '.jpg' '.Jpg' '.jpeg' and '.png'";
}
}
}
}
catch (Exception) { return View("Error"); }
return View();
}
查看:
@model DKWebPage.Models.AddImageModel
<div id="modal">
<div class="form-menu">
@using (Html.BeginForm("EditPhotos", "Admin", FormMethod.Post, new { @class = "form" }))
{
@Html.AntiForgeryToken();
<input value="Upload Images" type="file" multiple />
@ViewBag.Message
@Html.Label("Categories", new { @class = "dd-menu-label" })
@Html.DropDownListFor(u => u.ID, (SelectList)Model.List, "Select Category", new { @class = "dd-menu" })
<input type="submit" value="Upload Images" class="smallbutton" style="align-self: flex-end"/>
}
</div>
</div>
模型
public class AddImageModel
{
public IEnumerable<SelectListItem> List { get; set; }
public int ID { get; set; }
}