c# - 隐藏字段不随文件一起发布

标签 c# asp.net-mvc-3

我有以下 MVC 表单和 Controller 来上传具有给定 ID 的商品图片。由于某种原因将此表单提交给 Controller 时,id 为空。我检查了呈现的 HTML,网页上呈现了正确的 ID。

形式:

@using(Html.BeginForm(new{id = ViewBag.id})){

<input type="hidden" name="id" id="id" value="@ViewBag.Id"/>

<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" />
}

和 Controller :

[HttpPost]
public ActionResult AddImage(int merchandiseId, HttpPostedFileBase image)
<snip>

为什么提交此表单会导致 merchandiseId 为空?

最佳答案

因为你用错了名字。变化

<input type="hidden" name="id" id="id" value="@ViewBag.Id"/>

<input type="hidden" name="merchandiseId" id="id" value="@ViewBag.Id"/>

public ActionResult AddImage(int merchandiseId, HttpPostedFileBase image)

public ActionResult AddImage(int id, HttpPostedFileBase image)

关于c# - 隐藏字段不随文件一起发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14161839/

相关文章:

c# - 基于组合框值,更改刷新计时器

c# - 对单个表列而非整个实体的 WCF 数据服务权限

asp.net-mvc-3 - StringLength 与 MaxLength 属性 ASP.NET MVC 与 Entity Framework EF Code First

database - 为什么不能以这种方式将图像保存到数据库?

asp.net-mvc-3 - ASP.NET MVC3 : upload image and save it to database

c# - C# 中的密集图形应用程序(使用 .NET/Mono)

c# 是否可以在 Windows 窗体应用程序中显示实时网页?

asp.net-mvc - 找不到类型或命名空间名称 'DbContext'

javascript - 将 Html.Raw() 存储在 Javascript、ASP.NET MVC 3 的字符串中

c# - .net(C#)中是否有任何“非常丰富”的文本框? (WPF)