2015-05-10 85 views
-1

我創建多個模型獲取DropDownListFor選定值

public class MultipleModel 
{ 
    public Photo Photo { get; set; } 
    public Room Room { get; set; } 
} 

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所選項目(例如/ images/2 /,其中2 =來自DropDownListFor的ID)並添加到數據庫。我如何使用Html.BeginForm從DropDownListFor正確發送選定的項目? 我的觀點:

@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" /> 
} 

而且我的控制器,其中Room.IdformCollection始終= 0和int? id不工作,並返回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; 
        } 
       } 
      } 
+0

(1)在'BeginForm()'方法,'新{ID = Model.Room.Id}'被添加HTML屬性 - 它不清楚是什麼你想用這個(2)你的崗位上做方法簽名應該是'public ActionResult AddRoomImg(MultipleModel model,HttpPostedFileBase img)'和'model.Room.Id'將包含選定的值,'img'將包含該文件。 (3)你的''不是多個,所以你爲什麼使用'foreach'循環? (4)使用僅包含視圖中需要顯示/編輯的屬性的視圖模型 –

回答

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 
} 
+0

我不是想澄清任何事情,我瞭解OP正在嘗試做什麼,並且我給了他答案。 – warheat1990

0

由於您的問題是關於從dropdownListFor中將選定值轉換爲MultipleModel,首先您應該更新函數參數。

使用MultipleModel model將表單數據導入模型,HttpPostedFileBase img而不是FormCollection formCollection將圖像文件作爲對象。嘗試以下方法。

[HttpPost] 
public ActionResult AddRoomImg(MultipleModel model, HttpPostedFileBase img) 
{ 
    // To get the selected value from dropdownListFor 
    var selectedRoomId = model.Room.Id; 

    // do what you want to do with img object 
    // you will get img.name in the file object that you can rename 
}