【发布时间】:2020-03-15 16:46:01
【问题描述】:
抱歉,我是 ASP.NET MVC 的新手,我在 Home 视图文件夹(文件 Index.cshtml)中有这段代码:
@{
ViewBag.Title = "Home Page";
}
<div class="row">
<div class="col-md-4">
<h2>Select file</h2>
<p>
<input id="File1" type="file" />
</p>
<p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301865">Learn more »</a></p>
</div>
<div class="col-md-4">
<h2>Upload to SQL Server</h2>
<input type="button" title="Upload to SQL Server" value="Upload to SQL Server" onclick="location.href='@Url.Action("Upload2SS", "SystemLogs")'" />
</div>
</div>
当我单击“上传到 SQL Server”按钮时,我只想将 File1 值传递给控制器操作:
public RedirectToRouteResult Upload2SS(FormCollection form)
{
string filePath = form["File1"].ToString();
var data = GetDataTabletFromCSVFile(filePath);
return RedirectToAction("Index");
}
但是,我继续为 filePath 变量获取System.NullReferenceException;谁能告诉我我错过了什么?
【问题讨论】:
-
您好,您必须在“输入”中设置属性“名称”。这适用于 MVC 中的所有控件。
标签: c# asp.net-mvc visual-studio