.Form request returns null

I cannot get the values ​​in my controller, it returns null. Please help me find out what I did wrong.

below is my code

index.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>

      

And in my controller

[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();
}

      

+3


source to share


1 answer


try adding name to input field:

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

      



According to the W3C specification, every form input element must have the specified name attribute. Otherwise, this item will not be processed.

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

+4


source







All Articles