Reputation: 1289
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.
Upvotes: 0
Views: 960
Reputation: 10756
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.
Upvotes: 1