0

At the moment I either use a controller class or a formViewModel. The controller source method that i'd like to add the array property object too is as follows:

Does anyone know how i would add an array to this ActionResult so that from my View i can loop through my array. My array that I add will contain coordinates that are to be plotted on a map object from client side code.

To do this do i have to use a formViewModel and set my view to reference this? If i can simply pass the array with the existing code then great.

public ActionResult IndexSearch(int? page, string siteDescription) {

        const int pageSize = 10;

        //Get all Sites
        var allSites = _siteRepository.FindAllSites();

        //Get all Sites that contain the value in sitedescription
        var searchResults = (from s in allSites
                             where s.SiteDescription.StartsWith(siteDescription)
                             select s);

        //Return partial view that the ajax reults get loaded into.
        var paginatedSites = new PaginatedList<Site>(searchResults, page ?? 1, pageSize);

        return PartialView("SiteIndexSearchresults", paginatedSites);
    }

1 Answer 1

0

I don't understand what formViewModel you are talking about. In the sample code you provided the controller action is passing an IEnumerable<Site> collection to your view so that you could loop through it:

<% foreach (Site item in Model) { %>
    <div><%: item.SiteDescription %></div>
<% } %>

or even better use a display template for this site (~/Views/Home/DisplayTemplates/Site.ascx):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<SomeNs.Site>" 
%>
<div><%: Model.SiteDescription %></div>

And in your main view simply:

<%: Html.DisplayForModel() %>
Sign up to request clarification or add additional context in comments.

1 Comment

Basically I want to be able to reference a subset of the data in the model thats passed from Javascript. I need to populate a mapping control from the client side. I'm struggling to figure out how to loop through my data at the client side once the page has loaded. Cheers, J

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.