我创建了多个模型
public class Room
{
public int Id { get; set; }
public string NumberRoom { get; set; }
public virtual ICollection<Photo> Photo { get; set; }
}
public class Photo
{
public int Id { get; set; }
public string PhotoName { get; set; }
public int Roomid { get; set; }
public virtual Room Room { get; set; }
}
有两种不同的型号:
Submit
在点击DropDownListFor
在我的视图我想要上传图像的文件夹与来自DropDownListFor所选项目的名称(例如/图像/ 2 /,其中2 =从ID为@using Hotel.BusinessObject
@model MultipleModel
@using (Html.BeginForm("AddRoomImg", "Admin",
FormMethod.Post, new { enctype = "multipart/form-data", id = Model.Room.Id}))
{
<div>@Html.DropDownListFor(m=> m.Room.Id, ViewBag.roomlist as SelectList, "Select Room")</div>
<input type="file" name="img" />
<input type="submit" value="upload" />
}
),并添加到数据库。我怎样才能使用Html.BeginForm从DropDownListFor正确发送所选项目?
我的观点:
Room.Id
我的控制器formCollection
中的int? id
总是= 0且NULL
不起作用并返回 public ActionResult AddRoomImg()
{
ViewBag.roomlist = new SelectList(db.Room, "Id", "NumberRoom");
return View();
}
[HttpPost]
public ActionResult AddRoomImg(FormCollection formCollection, int? id)
{
foreach (string item in Request.Files)
{
HttpPostedFileBase file = Request.Files[item] as HttpPostedFileBase;
if (file.ContentLength == 0)
continue;
if (file.ContentLength > 0)
{
ImageUpload imageUpload = new ImageUpload { Width = 600 };
ImageResult imageResult = imageUpload.RenameUploadFile(file);
if (imageResult.Success)
{
//TODO: write the filename to the db
}
else
{
ViewBag.Error = imageResult.ErrorMessage;
}
}
}
typedef void *HIMAGE; // Image Handle
HIMAGE LoadImage(...);
答案 0 :(得分:1)
试试这个
[HttpPost]
public ActionResult AddRoomImg(MultipleModel model, HttpPostedFileBase img)
{
//your code here
}
或
[HttpPost]
public ActionResult AddRoomImg(MultipleModel model)
{
var image = Request.Files(0); //access your images
//rest of your code here
}