开发者

Add an Array object as a property of my existing Controller Class Method

开发者 https://www.devze.com 2023-01-28 12:43 出处:网络
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:

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 con开发者_开发百科tain 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);
    }


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() %>
0

精彩评论

暂无评论...
验证码 换一张
取 消