首页 > 解决方案 > 将值属性传递给控制器

问题描述

我有一个 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. 有人可以建议有什么错误吗?

标签: c#asp.net-mvcasp.net-mvc-4

解决方案


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

推荐阅读