How do I reference a jquery variable in a C# block in the view using ASP.NET MVC?
For example:
$(":input[#name='mydropdown']").change(function () {
var selection = $("#myselection").val();
pop($("#md"), <%= Model.choices[selection] %>);
});
Where the selection that is in my C# block is the same as the selection that is referred to in my jquery.
This is not possible to do. The C# code is executed before the HTML is sent to the user's browser, which is before jQuery gets loaded, which is before the variable selection has a chance to exist.
There are two approaches to work around this:
Dump all data that you care from Model.choices to a JavaScript variable; your JS code can then access that variable. This is simple and good if your data is not too large in volume.
Have the JS code make an AJAX request to the server to get whatever data it needs by passing the value of selection as a query string parameter.
Perhaps try Sharpkit plugin from jquery website:
http://plugins.jquery.com/project/SharpKit
You can't do this because of the browser (client) does not share any memory or state with the server what so ever
i.e.
the server executes the c# that renders the html and js
the browser downloads this and interprets it
the browser executes the javascript (no c#!)
I'd go with Jon's suggestion 1) as it will be more performant by negating the need for another callback to the server.
Long live ASP.NET MVC! :)
Related
I am having trouble wrapping my head around how Razor syntax works.
It is my understanding that it will allow you to embed server-side code directly into the page, but it keeps all of that from the client even though the client can interact with it.
I'm used to ASP.net web views and things of that nature - does the Razor syntax essentially do postbacks and all of the code is contained within the page, or is the way that the client interacts with the server-side code fundamentally different?
If this is too open-ended, please leave me comments as to how to focus the question more. I fear I don't yet understand enough to make finely-focused questions.
Thanks!
From my understanding:
1. How Razor syntax works:
You can see the Razor is similar to the scriplet in Web Form. For example, you want to get the Name of the Human class, in .cshtml file, you write:
<div>Name is #Model.Name</div>
And in aspx, you write:
<div>Name is <%= Human.Name %></div>
Both Razor and scriplet will be processed before returning the HTML files to client. Follow the above example, if you view the source of your HTML web page, you will see (Forte is just an example):
<div>Name is Forte</div>
Because the code was processed, so, what the client see is the HTML result only.
2.How do clients interact with server?
In Web Form, when you want to go to server (like handle click button event), you can generate the function to call by set OnClickListener for the button, and everything is done automatically for you.
However, in MVC.NET, you have to do it through Ajax call, or put your button in a <form>.
If you have any question, feel free to let me know :)
mvc runs all that stuff server side and provides the client with the resulting html/javascript generation.
Lets say you have this in a view
#{ var var1 = "Hello World"; }
<p>The value of var1 is: #var1</p>
Everything happens on the server
When a user is requesting this page the server gets the text above and sends it to the view engine.
Then the server runs the text line by line and "compiles" it.
Then the server snds the result back to the user:
The value of var1 is: Hello World
If the user makes a view source he will see only:
<p>The value of myMessage is: Hello World</p>
So the user on the client writes a simple URL , and the result he is getting is ONLY the line above.
Essentially it compiles the view like any other csharp/vb.net file. However, it does it on-the-fly (the first time you hit it, and the underlying file hasn't changed).
If you want to see what the generated code looks like, take a look at https://github.com/RazorGenerator/RazorGenerator This allows you to actually generate the same code as part of MSbuild and deploy it.
I have developed crawler in C#.
I am reading data from one page that is list page, It uses javascript for redirecting to next page.
function is - <a onclick="redirectToNextPage(PageID)">More</a>
How i can run this function in serverside and get url of the next page, so that by that url i can save that page.
I want to run javascript function in C# to get url of next page
You'll almost certainly need a headless browser to do that, not just running JavaScript code without the context it expects to run in. This question and its answer list some headless browsers that can be used from C# (not all of them have JavaScript support, though). That list may well be out of date now, but that's the term you need to search for.
Try https://javascriptdotnet.codeplex.com/.
It exposes Google V8 JS engine to CLI and also allows to CLI objects to be manipulated by JS
I think there is something that I don't understand as I proceed forward in my project of importing D3 visuals into Spotfire.
I currently have implemented a D3 visualization into Spotfire by using Awesomium's embedded browser and these lines:
string myString = System.IO.File.ReadAllText(#"C:\Users\leear\Desktop\SDK\SDK\Examples\Extensions\D3Visualizations\Gauge.html");
webView.LoadHTML(myString);
The HTML of Gauge.html contains javascript code that runs all the necessary things to create the visualization.
However, I want to be able to access/run C# functions and variables inside the Gauge.html file. I am envisioning something like this:
for (var value in <%=Spotfire.getCurrentColumn()%>) {gauge.redraw(value)}
My project is currently structured just for the C# code to read and write all the HTML from a file into Awesomium's embedded browser.
How would I do this? Where does aspx.NET fit into this, if at all?
Thanks
You can't; javascript is client-side, and ASP.NET is server side. ASP.NET basically generates HTML and javascript, and once that rendering is done the page is processed the client-side takes over, which it knows nothing of ASP.NET. It's perfectly fine for ASP.NET to spit out JS becuase it's RENDERING it, but the client-side PROCESSING of JS, which is where you are trying to call a server-side method knows nothing of ASP.NET. You'd have to move the logic to the client, or have the client talk to the server using javascript.
I can't advise further as I don't know what that method does.
"You can't; javascript is client-side, and ASP.NET is server side. ASP.NET basically generates HTML and javascript"....
This is absolutely correct. Client and server are two different worlds...
You have a couple of options if you need to get code-reuse.
Look into a C# to javascript compiler..and make sure your C# functions don't depend on anything related to the server or its runtime.
Otherwise, consider exposing your server functionality as a web service that takes input of some form and returns output as JSON for use on the client side.
I want to call a server side function which will run in the background of my website (while the user is navigating between the pages and continues to work normally).
I thought of writing the method in my Main.Master.cs page, call it from the client side of the content page, and display a popup at the end of the execution.
Is it possible?
How can I call Main.Master.cs method from content page client?
Thanks.
I had similar issue.
I used jQuery ajax with generic http handler returing json
I created handler and put my business logic there.
It return me the result in form of json
I iterated the json using jquery.
And created my html form that.
Edit 1
Here are some useful links
http://www.codeproject.com/Articles/203621/Call-HTTPhandler-from-jQuery-Pass-data-and-retriev
http://www.sharepointnutsandbolts.com/2010/11/sp2010-ajax-part-4-returning-json-from.html
Edit 2
You can also take benefit of jtemplate If you want to display something from the server to the page.
http://www.joe-stevens.com/2010/01/05/using-the-jtemplate-jquery-plugin-with-ajax-and-asp-net/
http://encosia.com/use-jquery-and-aspnet-ajax-to-build-a-client-side-repeater/
Yes it is possible if you are using Ajax in your application.
here is a detailed article on how to do this.
http://www.dotnetcurry.com/ShowArticle.aspx?ID=109
i was just playing around with javascript/jquery within mvc 3 and was just curious as to whether there was a way to pass in a C# variable to javascript and then modify from within the script. Though playing around with it, i noticed that it was not possible, as the variable passed in was just the value, and not the address.
Essentially, what i wanted to do was change a bool value from false to true when a html button is clicked. I figured i could do it through javascript but ran into the aforementioned problem. Is there anyway of doing this? Better yet, i'm sure there is a way, but is my design pattern flawed?
This is how ASP.Net/MVC is setup... No you can't modify server side C# variables in JavaScript. JavaScript is run in browser while C# page's code run on server. All C# classes are created and destroyed for each request and JavaScript code does runs at the different time (again destroyed after each page navigation via GET/POST).
There are multiple ways to pass data to/from JavaScript and easiest is render on GET and do submit (POST) to return changed fields to server.