0

While coding an assignment I've come to need to transfer data from the code-behind to the view so that I can parse that data with Javascript and build some HTML with it, and I've decided to use asp:HiddenField to that end.

However, it seems something goes wrong, since I get the error "The name "HiddenFieldData" does not exist in the current context".

I assume that I'm somehow not linking the view to the model correctly. Perhaps it's because I'm using a model that is not the appropriate cshtml.cs, but one that is "given" to the view via the controller. Truth be told, this is my first time with ASP.NET so it's very likely the problem is somewhere here.

The code in question, I've marked the trouble spots with '>>>>':

Controller -

public class saveController : Controller
    {
        // GET: Save
        public ActionResult SaveRoute()
        {
            saveModel model = new saveModel();
Model given >>>> return View(model);
        }
    }

Model -

public class saveModel
    {
        private DataMiner miner;
        public saveModel(string ip = "127.0.0.1", int port = 5400, int duration = 10, int interval = 1000)
        {
            // Initialize miner
            miner = new DataMiner(ip, port, duration, interval);
        }

        public void SaveRoute()
        {
            // Mine and retrieve data
            miner.Mine();
            double[][] data = miner.GetData();
            int lines = data.GetLength(0);
            int cols = data.GetLength(1);

            string[] str_data = new string[lines];
            for (int i = 0; i < lines; ++i)
            {
                // Turn double data into strings to write
                str_data[i] = data[i].ToString();
            }

            // Write to file
            System.IO.File.WriteAllLines(@"file1.txt", str_data);

            // Write values to HiddenField
            string values = String.Join(" ", str_data);
 Error here >>>> HiddenFieldData.Value = values;

            // Call JS function to load at
            ScriptManager.RegisterStartupScript(this, GetType(), "showDataMined", "showDataMined();", true);
        }
    }

View -

@model RESTful_Flight_Simulator.Models.saveModel
@{
    ViewBag.Title = "SaveRoute";
}

<html>
<head>
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
    <script type="text/javascript" language="javascript">
        function showDataMined()
        {
            var body = document.body
            var tbl  = document.createElement('table');
            tbl.style.width  = '100px';
            tbl.style.border = '1px solid black';

            for (var i = 0; i < 3; i++)
            {
                var tr = tbl.insertRow();
                for (var j = 0; j < 2; j++)
                {
                    if (i == 2 && j == 1) { break; }
                    else
                    {
                        var td = tr.insertCell();
                        td.appendChild(document.createTextNode('Cell'));
                        td.style.border = '1px solid black';

                        if (i == 1 && j == 1) {
                            td.setAttribute('rowSpan', '2');
                        }
                    }
                }
            }
            // Build title for table
            var title = document.createElement('h3');
            title.innerHTML = "Data mined:";
            // Finally, append title and table to body
            body.appendChild(document.createElement('hr'));
            body.appendChild(title);
            body.appendChild(tbl);  
        }
    </script>
</head>
<body>
HiddenField >>>> <asp:HiddenField id="HiddenFieldData" runat="server" value="" />
    <h2>Saving route...</h2>
</body>
</html>

Thanks ahead for any help!

  • 1
    You are mixing Webforms and MVC. That is not gonna work. Choose one and stick with that (preferrably MVC) – VDWWD May 28 '19 at 08:18
  • Possible duplicate of [Why can't I use server controls in ASP.net MVC?](https://stackoverflow.com/questions/9039019/why-cant-i-use-server-controls-in-asp-net-mvc) – Ahmed Yousif May 28 '19 at 08:23
  • It is recommended to not use asp webforms controls in MVC https://forums.asp.net/t/1765886.aspx?Is+it+a+right+way+to+add+the+ASP+controls+in+MVC+Application – Ahmed Yousif May 28 '19 at 08:26
  • I understand, thank you! If so, how would I go about transferring data to the DOM/JavaScript in run-time? – Ben Shabtai May 28 '19 at 08:56
  • Take the MVC tutorial provided by Microsoft, and you will understand the pattern and how to use it – ADyson May 28 '19 at 09:21
  • Seems like the answer is simply to use @model. Also, while I have you here, do you mind telling me how to start a script already written? Using Web Forms it would seem the answer would be to use RegisterStartupScript, but now I'm not sure. – Ben Shabtai May 28 '19 at 09:46
  • You can just put the call to the function in your actual script, like any normal JavaScript. e.g. `` – ADyson May 28 '19 at 10:00
  • I meant to call the script from the code-behind, since I need to load some data from an outside server before putting the data into a model member, and only then activating the script. – Ben Shabtai May 28 '19 at 10:17
  • You can use Razor code to inject the model data into the JavaScript – ADyson May 28 '19 at 11:40
  • Would you mind expanding on that, or linking to some source which shows how this could be done? I'm asking because as far as I know Razor code only activates upon loading the page. – Ben Shabtai May 28 '19 at 13:52
  • Exactly. You use the razor code to generate (parts of) the JavaScript which will eventually be sent to the browser, just like you use it to generate HTML. so you can, for example, only generate the JavaScript if certain conditions are met in the model data, and/or use razor code to inject variable values which will then become hard-coded elements of the JavaScript. This link gives an example: https://stackoverflow.com/a/5614995/5947043 – ADyson May 28 '19 at 18:24
  • In this case, my model is doing some computations in the background, which the JavaScript needs to wait to be done. I'm rather certain that unless I have a way of triggering a script already written in the page from the model, I would have to write and activate on page-load a JavaScript function that would periodically check whether a boolean in the model is set to true and if so then to activate the data loading function in question. Is there any better practice to do here? – Ben Shabtai May 28 '19 at 22:24
  • Your case is not really any different to any other. You said "which the JavaScript needs to wait to be done". Of course it will wait. The JavaScript is not executed until the page is received by the browser and rendered. The page is not received by the browser until after the server has sent it. The server does not send the page until after the server-side code (i.e. the action method in MVC) has finished executing. So there is no possibility for the JavaScript code **not** to wait for the server. – ADyson May 29 '19 at 07:50

0 Answers0