Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the code you've shared there's nothing emitting the model to the client, so there's currently no direct way for the JavaScript code to access it.</p> <p>Since you're binding the view to the model, the view can include it in various ways. It could be a series of hidden fields for the members of the model (not the model in its entirety, unless it can be represented as a string in its entirety). Something like this:</p> <pre><code>@Html.HiddenFor(x =&gt; x.SomeField) @Html.HiddenFor(x =&gt; x.AnotherField) </code></pre> <p>This would create two hidden <code>input</code>s for two fields on the model. Depending on how complex the model is, this could get cumbersome.</p> <p>You might also emit the model to the JavaScript code directly in a similar fashion:</p> <pre><code>var someField = @Model.SomeField; var anotherField = @Model.AnotherField; </code></pre> <p>Again, if the model is complex, this gets cumbersome quickly. Even if you try to build an actual JavaScript object from it:</p> <pre><code>var theModel = { someField : @Model.SomeField, anotherField : @Model.AnotherField }; </code></pre> <p>(Note also that I've seen Visual Studio get very confused when you mix razor syntax and JavaScript like this. Not so much in 2012 anymore, but a lot in 2010.)</p> <p>You might use something like <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" rel="nofollow">the <code>JavaScriptSerializer</code></a> to add a property on the model for a serialized version of itself. I've never done this before, but it should work. Something like this on the model:</p> <pre><code>public string SerializedCopy { get { return new JavaScriptSerializer().Serialize(this); } } </code></pre> <p>It might take some tweaking to get it to work, though.</p> <p>Finally, a particularly clean option which only requires another request to the server would be to have another action which just returns the JSON version of that model. Something like this:</p> <pre><code>public ActionResult SomeActionName() { // get the model somehow, then... return Json(AdsPrevModel); } </code></pre> <p>Your JavaScript code would then just need to call this action to get the JSON object representing the whole model:</p> <pre><code>var theModel = {}; $.get('@Url.Action("SomeActionName", "SomeController")', function (data) { // maybe do some error checking here? theModel = data; }); </code></pre> <p>Then if your actual view isn't actually <em>binding</em> anything to the model then the action which returns that view doesn't need to fetch the model and supply it to the view. The JavaScript code would get the model by calling this other action which returns JSON data instead of a view.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload