As a Java developer transitioning to .NET, I find myself delving into a .NET MVC2 project that requires a partial view for wrapping a widget. These JavaScript widgets come with JSON data objects that need to be populated by model data. Methods are then bound to events to update this data whenever changes occur either within the widget itself or in another widget.
The code snippet looks something like this:
MyController
:
virtual public ActionResult DisplaySomeWidget(int id) {
SomeModelView returnData = someDataMapper.getbyid(1);
return View(myview, returnData);
}
myview.ascx
:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SomeModelView>" %>
<script type="text/javascript">
//creates base widget object;
var thisWidgetName = new Widget();
thisWidgetName.updateTable = function() {
// UpdatesData
};
$(document).ready(function () {
thisWidgetName.data = <% converttoJSON(model) %>
$(document).bind('DATA_CHANGED', thisWidgetName.updateTable());
});
</script>
<div><%:model.name%></div>
I am unsure of how to effectively send the data as SomeModelView
and then utilize it to populate the widget, as well as converting it to JSON. While I have seen some straightforward methods to achieve this in the controller, I have not found a similar approach for the view. This seems like a basic question but I have been struggling for quite some time to streamline this process.