ASP.NET - Question about MVC2 using C# and ASP.NET

Asked By A S on 09-Aug-11 07:58 AM
can anybody give me example how I can return multiple ViewResults (List of objects) in a view (single web page) from controller?

Thank you in advance..
Ravi S replied to A S on 09-Aug-11 07:59 AM


We shipped ASP.NET MVC 1.0 last March.  Since then, almost 1 million developers have downloaded and used the final release, and its popularity has steadily grown month over month.

ASP.NET MVC 2 is the next significant update of ASP.NET MVC. It is a compatible update to ASP.NET MVC 1 – so all the knowledge, skills, code, and extensions you already have with ASP.NET MVC continue to work and apply going forward. Like the first release, we are also shipping the under an OSI-compliant open-source license.

ASP.NET MVC 2 Features

ASP.NET MVC 2 adds a bunch of new capabilities and features.  I’ll cover them in a lot more depth with this blog series.  Some of them include:

  • (posted: Jan 10th, 2010)
  • (posted: Jan 15th, 2010) 
  • Auto-Scaffold UI Helpers with Template Customization (post coming soon)
  • Support for partitioning large applications into “Areas” (post coming soon)
  • Asynchronous Controllers support (post coming soon)
  • Support for rendering sub-sections of a page/site using Html.RenderAction (post coming soon)
  • Lots of new helper functions, utilities, and API enhancements (post coming soon)
  • Improved Visual Studio tooling support (post coming soon)

How to download ASP.NET MVC 2

ASP.NET MVC 2 is designed to work with both VS 2008 / .NET 3.5, as well as with VS 2010 / .NET 4.  Supporting both versions means that you can start using it today without having to wait to upgrade to VS2010 / .NET 4. to download the ASP.NET MVC 2 release candidate for .NET 3.5 and VS 2008.  It can be installed side-by-side with ASP.NET MVC 1.0 on the same machine.

ASP.NET MVC 2 is a built-in component of VS 2010 / .NET 4 – which means you will not have to download or install anything to get it once you install any version of Visual Studio 2010.  The current public VS 2010 Beta 2 release includes the ASP.NET MVC 2 Preview 2 release.  The upcoming that will be available for download next month will have a more recent ASP.NET MVC 2 RC built-in.


We are always careful to make clear that ASP.NET MVC is an option with ASP.NET.  ASP.NET Web Forms continues to be the most widely used approach when building applications with ASP.NET, and the new ASP.NET 4 release contains significant improvements for ASP.NET Web Forms development (clean client IDs and CSS based control markup, better viewstate management, new data and charting controls, URL routing, SEO improvements, and much more).  You can learn more about these improvements within my ongoing

We’ll be improving and enhancing both the ASP.NET Web Forms and ASP.NET MVC programming models even further in future releases. Developers can and should choose to use whichever model feels most comfortable and natural to them. We’ll be publishing new videos and guidance on the shortly that helps provide additional guidance about each and how to pick the one that feels most comfortable to you. 

Hope this helps,

Ravi S replied to A S on 09-Aug-11 08:01 AM

Try this:

RedirectToAction("Edit", "Profile", new {id = 1, otherParam = "foo"});

This won't render view of another controller - but it will navigate to action of another controller (which is supposed to render view you are looking for).


Jitendra Faye replied to A S on 09-Aug-11 08:01 AM

For that follow these steps-

1) Create a ViewData class. This is just a POCO class with properties defined for each data item you want to show on the view (e.g. OpenCallCount)

2) Create a strongly typed view that uses this ViewData class.

3) Pass a new instance of your ViewData class, with the properties set, to your view.

This will help you avoid using magic strings everywhere (e.g. ViewData("OpenCallCount") = ... becomes myViewDataClass.OpenCallCount = ...)

You could probably tidy up the view by using two partial view classes, or making it slightly more generic, but it will do the job at the moment.

Sreekumar P replied to A S on 09-Aug-11 08:03 AM

i ended up just splitting up the results into multiple viewresults, and calling them asynchronously through jquery, like so:

function run_search(url) {
  var search_url = url.replace('?', '/facettree?');
  $.get(search_url, function (html) {
  search_url = url.replace('?', '/booksearchresults?');
  $.get(search_url, function (html) {

Then, facets that are clicked on go straight to this method, and a timer on the query box adds the query to the url and calls this, after a pause of half a second.

Riley K replied to A S on 09-Aug-11 08:03 AM
You can pass list of objects from View to Controller

First javascript function:
<script src="<%= Url.Content("~/Scripts/jquery-1.3.2.min.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/jquery.json-2.2.min.js") %>" type="text/javascript"></script>
<script type="text/javascript">
  function savePayment() {
  //Creating some test data
  var lineItems = new Object();
  lineItems.Entrys = new Array();
  lineItems.Entrys[0] = new Object({ Approved: "1.0", Deductible: "1.0", Coinsurance: "1.0", PaymentAmount: "1.0", VisitId: "1.0", VisitChargeId: "1.0" });
  lineItems.Entrys[1] = new Object({ Approved: "2.0", Deductible: "2.0", Coinsurance: "2.0", PaymentAmount: "2.0", VisitId: "2.0", VisitChargeId: "2.0" });       
  //Posting them to server with ajax
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    url: '<%=Url.Action("SavePayment", "Home") %>',
    dataType: 'json',
    data: $.toJSON(lineItems),
    success: function(result) {
    if (result) {
    else {
With this code we are posting a javascript object (lineItems), which contains an Array (Entrys) to the server. We are doing this in JSON format (this script requires jQuery and jQuery JSON plugin, which you can download from here: ASP.NET MVC can't bind JSON by default, so we need to write JSON model binder:
/// <summary>
/// Model binder which allows binding JSON data to objects
/// </summary>
public class JsonModelBinder : IModelBinder
  #region IModelBinder Members
  public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
  if (controllerContext == null)
    throw new ArgumentNullException("controllerContext");
  if (bindingContext == null)
    throw new ArgumentNullException("bindingContext");
  var serializer = new DataContractJsonSerializer(bindingContext.ModelType);
  return serializer.ReadObject(controllerContext.HttpContext.Request.InputStream);
Now we need to prepare container for list:
public class PaymentPostingChargeEntrys
  public List<PaymentPostingChargeEntry> Entrys { get; set; }
And the last thing to do is controller action:
public ActionResult SavePayment(PaymentPostingChargeEntrys lineItems)
  return Json(true);
That should give you what you want (of course after some modifications). I hope it will help you.
Cos mos replied to A S on 09-Aug-11 08:04 AM
This link might help
Sreekumar P replied to Sreekumar P on 09-Aug-11 08:05 AM
for this form

  <div id="query">
    <%= Html.TextBox("q")%>
  <div id="filter"/>
  <div id="results-list"/>

Radhika roy replied to A S on 09-Aug-11 12:21 PM


There's always just one response for a request.

But if you are trying to send both your viewOpenCalls and viewMyOpenCalls objects to one view, then you can do that with making your view have a model that'll hold both of them.

Like this :

//My VB is a bit rusty so I'm writing this in C#
class Calls {
public yourDataType OpenCalls { get; set; }
public yourDataType MyCalls { get; set; }

In your controller action :

return View(new Calls { OpenCalls
= viewOpenCalls,  MyCalls = viewMyOpenCalls })

//I gues in VB it would be like this :
Dim viewOpenCalls = callRepository.FindAllOpenCalls()
Dim viewMyOpenCalls = callRepository.FindAllMyCalls(Session("LoggedInUser"))
Return View(New Calls _
With {.OpenCalls = viewOpenCalls, .MyCalls = viewMyOpenCalls})

In your view make sure it's model is type of the Calls class.

<%@ Page Inherits="System.Web.Mvc.ViewPage<Calls>"

And now you can access the properties with <%=Model.OpenCalls %> and <%=Model.MyCalls %>

Hope this will help you.