I am working in a jquery mobile website using webmatrix and razor but I don't know how to call a razor function passing a javascript variable as parameter:
<script type="text/javascript">
var param = $("#mycombo").val();
@getPrice(param); // param does not exist in the current context
@getPrice(@:param); //opening "(" is missing the corresponding closing ")".
@{
getPrice(
@:param
); //Cannot convert lambda expression to type 'string' because it is not a delegate type
}
</script>
@functions
{
string getPrice(string id)
{
var db = Database.Open("DB");
var extra = db.QuerySingle("Select * from productVariety Where productVarietyId = @0", id);
return extra.price;
}
}
Other methods to pass the selected value of the combobox to razor are welcome!
I'm pretty new to the whole Javascript scene aswell, but I think you will need to do a AJax request. Using something like Jqueries Ajax methods. Then from the form handling the request pull the values from the UrlData or Page Data object. Sorry I can't provide
more info hopefully I set you on the right path. This arcticle although a bit diffrerent is using the method I'm refering to.
I'm am unable to get the alert to fire off of the $.post aswell I am however getting the data back fine in my recieiving page. I created a seperate page with the code to execute on it. You might try creating a seperate page to hit on the $.post so you could
debug the page in visual studio and see if its even getting hit. Also have you checked in Firebug or Chrome's Javascript console for any errors?
Edit:
After reading the docs a bit more I think the reason the alert isnt getting fire is cause the page being called doesnt return any data back.
Edit:
Acutally I was wrong. The callback is getting fired its just not getting back what it expects. Change the 'json' to 'html' and the alert will fire.
$.post('BrowserService.cshtml', { data : 'mydata' },
function (data) {
alert("Data Inserted");
},'html');
epb86
Member
8 Points
5 Posts
jQuery inside Razor
Apr 05, 2011 01:35 PM|LINK
Hi!
I am working in a jquery mobile website using webmatrix and razor but I don't know how to call a razor function passing a javascript variable as parameter:
<script type="text/javascript">
var param = $("#mycombo").val();
@getPrice(param); // param does not exist in the current context
@getPrice(@:param); //opening "(" is missing the corresponding closing ")".
@{
getPrice(
@:param
); //Cannot convert lambda expression to type 'string' because it is not a delegate type
}
</script>
@functions
{
string getPrice(string id)
{
var db = Database.Open("DB");
var extra = db.QuerySingle("Select * from productVariety Where productVarietyId = @0", id);
return extra.price;
}
}
Other methods to pass the selected value of the combobox to razor are welcome!
Thanks in advance
Jessemlay
Member
24 Points
14 Posts
Re: jQuery inside Razor
Apr 05, 2011 08:21 PM|LINK
I'm pretty new to the whole Javascript scene aswell, but I think you will need to do a AJax request. Using something like Jqueries Ajax methods. Then from the form handling the request pull the values from the UrlData or Page Data object. Sorry I can't provide more info hopefully I set you on the right path. This arcticle although a bit diffrerent is using the method I'm refering to.
http://www.mikesdotnetting.com/Article/155/WebMatrix-And-jQuery-Forms
Edit:
Try this it worked quite well for me
$(document).ready(function () { $.post('BrowserService.cshtml', {Json: Data}, function (data) { },'json'); }); </script>Then in the file BrowserService.cshtml you can access the values using the Request.Params["name"].
@{ var db = Database.Open("StarterSite"); var insertQuery = "INSERT INTO ClientInfo (BrowserName, BrowserVersion, BrowserLayoutEngine, OperatingSystem) VALUES (@0, @1, @2, @3)"; db.Execute(insertQuery, Request.Params["browserName"], Request.Params["browserVersion"], Request.Params["browserLayoutEngine"], Request.Params["operatingSystem"]); }epb86
Member
8 Points
5 Posts
Re: jQuery inside Razor
Apr 07, 2011 09:51 AM|LINK
First of all, thanks for your reply!
I have tried to make the ajax post but no success in the $.post call, nothing seem to happen:
@functions
{
string getPrice(string id)
{
int eid =Request.Params["myid"].AsInt(); // it is taking a 0 as value
var db = Database.Open("mLunchDB");
var extra3 = db.QuerySingle("Select * from productVariety Where productVarietyId = @0", eid);
}
}
<script type="text/javascript">
$(document).ready(function ()
{
var eid = $("#CurrentExtraId").val();
alert(eid); // this value is ok
$.post('buyProductDialog.cshtml', {myid: eid}, function (data) { alert("Data Loaded"); },'json'); //nothing happen, no alert
});
Any ideas?
There is no other way to pass the selected value of a dropdown to razor?
Thanks for the help!
Jessemlay
Member
24 Points
14 Posts
Re: jQuery inside Razor
Apr 07, 2011 01:29 PM|LINK
Is the $.post calling to itself or another page?
epb86
Member
8 Points
5 Posts
Re: jQuery inside Razor
Apr 07, 2011 01:30 PM|LINK
to itself
Jessemlay
Member
24 Points
14 Posts
Re: jQuery inside Razor
Apr 07, 2011 01:45 PM|LINK
I'm am unable to get the alert to fire off of the $.post aswell I am however getting the data back fine in my recieiving page. I created a seperate page with the code to execute on it. You might try creating a seperate page to hit on the $.post so you could debug the page in visual studio and see if its even getting hit. Also have you checked in Firebug or Chrome's Javascript console for any errors?
Edit:
After reading the docs a bit more I think the reason the alert isnt getting fire is cause the page being called doesnt return any data back.
Edit:
Acutally I was wrong. The callback is getting fired its just not getting back what it expects. Change the 'json' to 'html' and the alert will fire.
$.post('BrowserService.cshtml', { data : 'mydata' }, function (data) { alert("Data Inserted"); },'html');epb86
Member
8 Points
5 Posts
Re: jQuery inside Razor
Apr 07, 2011 02:25 PM|LINK
It is giving 500 Internal Server Error also posting to another page...
Dont know why
Jessemlay
Member
24 Points
14 Posts
Re: jQuery inside Razor
Apr 07, 2011 02:56 PM|LINK
I'm pretty sure your getting the 500 because you using
@functions { methods }use this to enclose the code instead
@{ //instead of a return write to the reponse object Json.Write(value,Response.Output); }Mikesdotnett...
All-Star
154955 Points
19872 Posts
Moderator
MVP
Re: jQuery inside Razor
Apr 07, 2011 03:08 PM|LINK
Have a look at the example here: http://www.mikesdotnetting.com/Article/160/WebMatrix-Working-With-The-JSON-Helper
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter