简体   繁体   English

在MVC 4 Razor中使用Javascript添加会话

[英]Add Session using Javascript in MVC 4 Razor

I'm writing application which upload file, first I need some configuration for file storage i read the first Ten rows and show them to user, user provide configuration and I'll upload the file . 我正在编写上传文件的应用程序,首先我需要对文件存储进行一些配置,我阅读了前十行并将其显示给用户,用户提供了配置,然后我将上传文件。

i tried this 我尝试了这个

<script>
document.getElementById('file').addEventListener('change', readSingleFile, false);
function readSingleFile(evt) {
    var FileBlock = "";   
    var contents="";
    var f = evt.target.files[0];
    if (f) {
        var r = new FileReader();
        r.onload = function (e) {
            contents = e.target.result;
            var Rows = contents.toString().split("\n", 10);
            FileBlock = Rows[0];
             for (var i = 1 ; i < Rows.length; i++) {
                 FileBlock += "|" + Rows[i];
             }
             alert(contents);

            @{
                Session["Test"] = @:contents+"";
             }

            OpenDockPanelBySumbitForm("ImporterPanel", "form",FileBlock);
            Hide_ContentZone_Panel();
        }
        r.readAsText(f);      
    } else {
        alert("Failed to load file");
    }
}

this piece of code not work 这段代码不起作用

@{
   Session["Test"] = @:contents+"";
 }

if anyone Can Help I'll be grateful, Thanks in participation. 如果有人可以帮助我将不胜感激,谢谢您的参与。

I found that there is no way to do that, because C# Code compiled first before Javascript, so C# will not be able to see Javascript code while the opposite is possible. 我发现没有办法做到这一点,因为C#代码首先在Javascript之前编译,所以C#将无法看到Javascript代码,而可能相反。

one way to solve is move your script to another folder -plugins - to insure that it will compile first. 一种解决方法是将脚本移动到另一个文件夹-plugins-以确保它首先被编译。

check Source here 在此处检查源

Asp.net code is running server-based dynamic work in Javascript. Asp.net代码正在Javascript中运行基于服务器的动态工作。 JavaScript assign values ​​to variables based on the server can not. JavaScript无法根据服务器为变量分配值。 However, keeping in hidden variables you can use; 但是,保留隐藏变量可以使用;

http://i.hizliresim.com/aq336d.png http://i.hizliresim.com/aq336d.png

<input type="hidden" id="hdnsession" name="sessionData" value=" @ViewBag.session??"" " />
public ActionResult test(string sessionData){
Session["Test"] = sessionData;
ViewBag.session = Session["Test"];
return View();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM