I am making an AJAX call (with jQuery) to retrieve a PartialView
. Along with the HTML I'd like to send back a JSON representation of the object the View is displaying. The crappy way that I've been using is embedding properties as hidden inputs in the HTML, which quickly becomes unwieldy and tightly couples far too much stuff.
I could just send the JavaScript in a <script>
tag after the HTML, but I'm really anal about keeping those things separate. That would look like this:
<%@ Control Language="C#" AutoEventWireup="true" Inherits="System.Web.Mvc.ViewUserControl<Person>" %>
<div class="person">
First Name: <%= Html.TextBox("FirstName", Model.FirstName) %>
Last Name: <%= Html.TextBox("LastName", Model.LastName) %>
</div>
<script type="text/javascript">
// JsonSerialized object goes here
</script>
Another option I considered is to make a second AJAX call to an action that returns JsonResult
, but that also feels like bad design.