【问题标题】:Request .Form returns null请求 .Form 返回 null
【发布时间】:2014-11-27 07:12:49
【问题描述】:

我无法检索控制器中的值,它返回 null。请帮助我找出我做错了什么。

下面是我的代码

索引.aspx

<form id="form1" method="post" action="/Sample/Index" enctype="multipart/form-data">
<div>
    <input  type="text" id="PcId" value=<%=Model.PcId %> /></div>
    <input type="file" value="Browse" id="file"/>
    <input type="submit" id="submit" value="Save"/></div>
</form>

在我的控制器中

[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
    string PcId = Request.Form["PcId"];
    List<string> fileConfigData = new List<string>();

    if (file != null && file.ContentLength > 0)
    {
        string FolderPath = mPath.GetFolderPath();
        var fileName = Path.GetFileName(file.FileName);
        string filePath = Server.MapPath(FolderPath + PcId) + "\\" + fileName;
        file.SaveAs(filePath);
    }
    return view();
}

【问题讨论】:

  • 你关闭了&lt;div&gt; 2 次。

标签: c# html asp.net


【解决方案1】:

尝试在您的输入字段中添加名称:

<input  type="text" id="PcId" name="PcId" value=<%=Model.PcId %>

根据 W3C 规范,每个表单输入元素都应该有一个指定的名称属性。否则不会处理该元素。

http://www.w3.org/TR/html401/interact/forms.html#successful-controls

【讨论】:

    猜你喜欢
    • 2019-08-26
    • 2017-03-12
    • 2016-03-31
    • 2023-03-07
    • 2020-12-12
    • 2015-08-17
    • 2022-01-04
    • 2017-01-21
    • 2018-02-23
    相关资源
    最近更新 更多