I am developing MVC application in which I want to pass the Variable to the JScript which is in that view itself.
How to do that?
I want to send the value of AccessMode to the Javascript.
I have declared that Variable but don’t know how to use it in JScript.
<script src="@Url.Content("~/Scripts/jquery-1.8.0.min.js")" type="text/javascript"></script>
@model CRMEntities.Settings
@{
ViewBag.Title = "CustomSettings3";
//Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>CustomSettings3</h2>
<!DOCTYPE html>
<html>
<head>
<title>CustomSettings3</title>
</head>
<body>
@{string AccessMode; }
@{string Ratings; }
<div class="editor-label">
@Html.LabelFor(model => model.AccessMode)
</div>
<div class="editor-field">
@* @Html.EditorFor(model => model.AccessMode)*@
@Html.DropDownList("AccessMode")
@Html.ValidationMessageFor(model => model.AccessMode)
//Assigning value.
AccessMode = model.AccessMode
</div>
<div class="editor-field">
@Html.DropDownList("RatingsCount")
@Html.ValidationMessageFor(model => model.RatingsCount)
@{
Ratings = Html.DropDownList("RatingsCount");
}
</div>
<p>
<input type="button" value="Save" class="SaveSettings"/>
</p>
</body>
</html>
<script type="text/javascript">
$(document).ready( function(){
$('.SaveSettings').click(function ()
{
$.ajax
({
type: 'post',
url: '@Url.Content("~/Settings/SaveBasicSettings")',
dataType: 'json',
data:
{ 'AccessMode': "@AccessMode" },
success: function (data)
{
}
});
});
});
</script>

Given that
AccessModeis just a string variable defined in your view, and your JS is embedded in the same view, you can simple write the value of the variable to your JS in the standard way:Another way you can do this once you’ve moved your JS to a seperate file (if you choose to do so) would be to store the AccessModel value as a data atribute on the editor field.
That way your javascript can simply pick it up as it would any other attribute on a page.
Update:
The way you’re assigning the value needs adjusting also: