【问题标题】:Get DropDownListFor selected value in multiple model获取 DropDownListFor 多个模型中的选定值
【发布时间】:2015-05-10 15:02:06
【问题描述】:

我创建多个模型

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=id 来自 DropDownListFor)并添加到数据库。如何使用 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" />
}

还有我的控制器,其中formCollection 中的Room.Id 始终=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;
                    }
                }
            }

【问题讨论】:

  • (1) 在BeginForm() 方法中,new { id = Model.Room.Id } 正在添加一个 html 属性-不清楚您要对此做什么 (2) 您的 POST 方法签名应该是 public ActionResult AddRoomImg(MultipleModel model, HttpPostedFileBase img) 和 @ 987654334@ 将包含选定的值,img 将包含文件。 (3) 你的&lt;input type="file" ..&gt; 不是多个,那你为什么要使用foreach 循环呢? (4) 使用仅包含您需要在视图中显示/编辑的那些属性的视图模型

标签: asp.net asp.net-mvc html.dropdownlistfor


【解决方案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
}

【讨论】:

  • 我不想澄清任何事情,我了解 OP 想要做什么,我给了他答案。
【解决方案2】:

由于您的问题是关于从 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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    相关资源
    最近更新 更多