Reputation: 46222
I am using getJSON and what I like to pass as part of the url is the model that I have in my View (I am using MVC C#)
$.getJSON(url, function (data) {
});
I am wondering if there is a way to do this.
@model PVC.Domain.Lab.Models.ModelMain
Upvotes: 0
Views: 2000
Reputation: 1038720
You could send it as a JSON request:
@model MyViewModel
...
<script type="text/javascript">
var model = @Html.Raw(Json.Encode(Model));
$.ajax({
url: '@Url.Action("someaction")',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(model),
success: function(result) {
alert('success');
}
});
</script>
Note that Visual Studio's syntax highlighting might underline the var model = @Html.Raw(Json.Encode(Model));
line with a red squiggle telling you that there's an error. Feel completely free to ignore this error and run your application which will work.
Also notice that if you want to limit the network usage it would be better to send only the id of the model in the AJAX request and have your server retrieve this model using the id from wherever you retrieved it initially.
Upvotes: 2
Reputation: 747
The way to do this would be to use inline JavaScript to extract the part of your model you want to use in your url.
<script>
var myUrl = '/path/to/action/@Model.Id/whatever else';
// Then make your call using the constructed URL
</script>
Upvotes: 0
Reputation: 5283
In this case you have to use .json
file...
$.getJSON("yourpage.json", function (data) {
});
Upvotes: -1