c# - 将值属性传递给控制器
问题描述
我有一个 txt 文件,我需要将其上传到网页并进一步处理。我的解决方案是hidden
在视图中有一个标签并在控制器中获取它的值。我使用 进行了上传javascript
,它按预期工作,并且在运行脚本后我为标记分配了一个值:
<input id="filecontents" name="configurationstring" type="hidden" value="{
"ID": 0,
"Updated": "\/Date(1524153092965)\/",
"Countries": [11, 12, 15, 16, 23, 33, 38, 42, 43, 48, 49, 52, 57, 59, 62, 63, 66, 68, 69, 70, 73, 79, 80, 86, 87, 88, 90, 95, 103, 104, 106, 107, 108, 109, 113, 115, 116]
}">
然后我有 and ActionLink
,它从控制器调用这个方法:
public ActionResult getConfig(string configurationstring)
{
//perform computations
return View()
}
但是,不知何故,在控制器端configurationstring
总是null
. 有人可以建议有什么错误吗?
解决方案
html:
<form action="home/Index" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" />
</form>
C#:
public class ConfigFile
{
public DateTime Updated { get; set; }
public int ID { get; set; }
public List<int> Countries { get; set; }
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
string inputString = (new StreamReader(file.InputStream)).ReadToEnd();
JavaScriptSerializer jss = new JavaScriptSerializer();
var configFile = jss.Deserialize<ConfigFile>(inputString);
}
return View();
}
推荐阅读
- python - 如何使用flask框架将数据发送到html工作表
- node.js - 异步等待所有承诺
- c# - BigQuery 流式插入 - 估计插入大小以避免超出限制
- bootstrap-4 - 整行的垂直内容对齐
- xml - 点燃身份验证值显示“关闭”,但已启用
- javascript - 更改 AngularJS 中的嵌套对象值会更改所有其他值
- python - 如何将本地 mysql 数据库上传到 heroku 应用程序?
- java - 应用了java AtomicInteger accumAndGet
- angular - 转换为 PWA 后,我们的网站是否可以移动访问?
- python - 如何在 Python 中使用 turtle 模块播放 GIF?