gpt4 book ai didi

asp.net-mvc - 如何将其他表单数据与 MVC 文件上传一起传递?

转载 作者:行者123 更新时间:2023-12-04 22:12:51 25 4
gpt4 key购买 nike

我正在尝试在 MVC 中实现文件上传。我有以下有效的代码。

@using (Html.BeginForm("ActioName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
<input type="file" name="file" />
<input type="submit" value="OK" class="button" />
</div>
}

[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
// Verify that the user selected a file
if (file != null && file.ContentLength > 0)
{
//do something here...
}
}

现在我想添加一个下拉框(以选择文件类型)并将该值与文件一起发送到我的 Controller 。我该怎么做(连同文件一起发送其他表单数据)?

最佳答案

您应该能够将它们添加到 View 中,将它们包含在 POST 中并让 MVC 处理模型绑定(bind):

@using (Html.BeginForm("ActioName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
<input type="file" name="file" />
<select name="fileType">
<option value="JPG">Photo</option>
<option value="DOC">Word</option>
</select>
<input type="submit" value="OK" class="button" />
</div>
}

[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file, string fileType)
{
//Validate the fileType

// Verify that the user selected a file
if (file != null && file.ContentLength > 0)
{
//do something here...
}
}

关于asp.net-mvc - 如何将其他表单数据与 MVC 文件上传一起传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20077923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com