0

Based on my understanding i found that we can code from the View also. So i have a list which is populated in View using foreach loop.

@foreach (var item in Model)
{
@Html.Raw(item.Name)<br />
@Html.Raw(item.Price)<br />
@Html.Raw(item.Description)<br />
@Html.Raw(item.Image)
<hr />
}

I also have a dropdown list outside the foreach loop where i want to sort using the price.
I can get this action done by passing the model to the controller but here the page refreshes completely, which is something i dont want to happen. Inturn i wanted only the Model to be sorted dynamically say using a javascript to refine the items in Model which has a list. Any help would be appreciated.

Shiva Saurabh
  • 1,281
  • 2
  • 25
  • 47

1 Answers1

1

What I think you are trying to is something that cannot be done.

What I think you are trying to do is refresh a page to reorder a list using the Razor syntax without a page refresh or HTTP call to the server. This is not possible because the View in the ASP.NET MVC world is cshtml definition which is going to execute once per call. You need something running on the client side to refresh the page (ie JavaScript).

A good idea is to output the contents of your model as Json (which you could do in a standard MVC controller (without an associated view) returning a JsonResult using the MVC controller Json function. You could also use a WebApi controller which can produce Json nativly.

You will then need to have some code on the client side to look at that model and reorder it in response to your client interactions. Angular.js and Backbone.js are good contenders for this but there are lots of others.

or you could use jQuery.

Community
  • 1
  • 1
AlexC
  • 10,676
  • 4
  • 37
  • 55
  • Right now the methodology i am using is going to controller and again fetching the same records from the existing model, reordering and putting it in View. But because the page is getting refreshed, i was asked to validate it on the view side itself. thanks for the answer. Let me have a look athe the client interaction sites and fetching the solution with the links you provided. @AlexC – Shiva Saurabh Nov 27 '13 at 12:11