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
HI


ASP.NET MVC 2

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 http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=37423 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:

  • http://weblogs.asp.net/scottgu/archive/2010/01/10/asp-net-mvc-2-strongly-typed-html-helpers.aspx (posted: Jan 10th, 2010)
  • http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx (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.

http://www.microsoft.com/downloads/details.aspx?FamilyID=3b537c55-0948-4e6a-bf8c-aa1a78878da0&displaylang=en 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 http://weblogs.asp.net/scottgu/archive/2009/12/17/visual-studio-2010-and-net-4-0-update.aspx that will be available for download next month will have a more recent ASP.NET MVC 2 RC built-in.

ASP.NET Web Forms + ASP.NET MVC

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 http://weblogs.asp.net/scottgu/archive/2009/08/25/vs-2010-and-net-4-series.aspx.

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 http://www.asp.net/ 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
HI

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).

refer

http://stackoverflow.com/questions/879852/asp-net-mvc-display-a-view-from-another-controller

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
Hi,

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) {
    $('#filter').html(html);
  });
  search_url = url.replace('?', '/booksearchresults?');
  $.get(search_url, function (html) {
    $('#results-list').html(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
  $.ajax({
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    url: '<%=Url.Action("SavePayment", "Home") %>',
    dataType: 'json',
    data: $.toJSON(lineItems),
    success: function(result) {
    if (result) {
      alert('Success');
    }
    else {
      alert('Failure');
    }
    }
  });
  }
</script>
 
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: http://code.google.com/p/jquery-json/). 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);
  }
  #endregion
}
 
Now we need to prepare container for list:
 
[DataContract]
[ModelBinder(typeof(JsonModelBinder))]
public class PaymentPostingChargeEntrys
{
  [DataMember]
  public List<PaymentPostingChargeEntry> Entrys { get; set; }
}
 
And the last thing to do is controller action:
 
[AcceptVerbs(HttpVerbs.Post)]
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

http://stackoverflow.com/questions/1599821/return-multiple-views-to-one-actionresult-with-asp-net-mvc
Sreekumar P replied to Sreekumar P on 09-Aug-11 08:05 AM
for this form

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

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.