Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC3 - Compress model in viewbag
    primarykey
    data
    text
    <p>I have an MVC3 view that's design to run reports for our clients. In the View that lets you select the report options, I pass a large list of our clients (~3K). I'm rendering it like this: </p> <pre><code>@{Html.RenderPartial("_ClientSelector", ViewBag.Clients as IEnumerable&lt;Client&gt;);} </code></pre> <p>The _ClientSelector partial looks like this:</p> <pre><code>&lt;select id="clients" class="clientList" multiple="multiple"&gt; @foreach (Client client in Model) { var prefix = client.Parent == null ? "" : String.Format("{0}-({1}) / ", client.Parent.Name, client.Parent.Id); &lt;option value="@client.Id"&gt;@prefix@client.Name-(@client.Id)&lt;/option&gt; } &lt;/select&gt; </code></pre> <p>I'm using the jQuery multiselect library to turn this into a nice visual display.</p> <p>My question is, is it possible to compress this list server side? By far, the slowest part of this view is loading this list of clients. Using PageSpeed in chrome, it suggests compressing the html to make it load faster, is that possible? If this needs to be in it's own controller action instead of putting it in the ViewBag, I don't mind doing that. I just want to see if there is a way I can speed this page up.</p> <p>Edit:</p> <p>I started playing around with the templates you have below. When I build json manually, it works. However when I try to get it from the server using $.getJSON, it never fires on the results. Using debug tools in Chrome, I see the response coming back, and it looks like valid json. Can you spot something wrong with this?</p> <pre><code>public ActionResult ClientList() { var clients = reportRepository.GetClientList(); IList&lt;object&gt; model = new List&lt;object&gt;(); foreach (Phoenix.Models.Client c in clients) { var prefix = c.Parent == null ? "" : String.Format("{0}-({1}) / ", c.Parent.Name, c.Parent.Id); model.Add(new { value = c.Id.ToString(), text = String.Format("{0}{1}-({2})",prefix,c.Name,c.Id) }); } return Json(model, JsonRequestBehavior.AllowGet); } </code></pre> <p>And in the view:</p> <pre><code>&lt;script type="text/javascript"&gt; $(function () { $.getJSON("/Report/ClientList", null, function (data) { $("#templateOptionItem").tmpl(data).appendTo("#clients"); }); }); &lt;/script&gt; &lt;script id="templateOptionItem" type="test/html"&gt; &lt;option value=\'{{= value}}\'&gt;{{= text}}&lt;/option&gt; &lt;/script&gt; &lt;select id="clients" class="clientList" multiple="multiple"&gt; &lt;/select&gt; </code></pre> <p>--This was another attempt using the $.ajax method, but it too does not fire on the results </p> <pre><code>$.ajax({ url: "/Report/ClientList", dataType: "json", success: function (data) { $("#templateOptionItem").tmpl(data).appendTo("#clients"); } }); </code></pre> <p>This is the example that works:</p> <pre><code>var Clients = [{ value: 1, text: "test123" }, { value: 2, text: "123test" }] $("#templateOptionItem").tmpl(Clients).appendTo("#clients"); </code></pre> <p>After much tweaking, I finally got something to work:</p> <pre><code>$.ajax({ url: "/Report/ClientList", dataType: "json", complete: function (data) { $("#templateOptionItem").tmpl(JSON.parse(data.responseText)).appendTo("#clients"); } }); </code></pre> <p>I'm still not sure why it would not fire on the on the success event, but using the complete event and doing a JSON.parse on the responseText seems to have done the trick. </p>
    singulars
    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.
 

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