Question

to all, I realy need help. I am new in SolrNet and beginer in asp.net mvc 4. My project is to use SolrNet and view results in web app created in asp.net mvc 4. So, for begin I whant to just do simple query from SolrNet and display it in web,

So far I have create this:

Start new empty MVC project with name: SOLRTest

From Package Manager Console I have done this: Instal-Package SolrNet -Version 0.4.0-beta2

In HomeController i use this code:

using System;
using System.Web.Mvc;
using Microsoft.Practices.ServiceLocation;
using SOLRTest.Models;
using SolrNet;

namespace SOLRTest.Controllers
{
    public class HomeController : Controller
    {

       public ActionResult Index()
       {
          try
          {
              var solr = ServiceLocator.Current.GetInstance<ISolrReadOnlyOperations<Customer>>();
            SolrQueryResults<Customer> rezultati = solr.Query(new SolrQueryByField("customer", "INTS"));

              return RedirectToAction("Index", "Home");
          }
          catch (Exception ex)
          {
              string error = ex.Message;
          }

          return View();
       }
    }
}

In Models i use this code:

using SolrNet.Attributes;

namespace SOLRTest.Models
{
    public class Customer
    {
    [SolrField("customer")]
    public string customer { get; set; }
    }
}

In View i use this code:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="SOLRTest.Helpers" %>
<%@ Import namespace="SOLRTest.Models" %>

<!DOCTYPE html>

<html>
<body>
   <div>
      test
   </div>
</body>
</html>
    <% foreach (var izpis in SOLRTest.Models.Customer)
       { %>
           <li>
               <ul>
                     <%= Html.SolrFieldPropName<Customer>(izpis) %>                 
               </ul>

           </li> 
    <% } %>

For Helpers I use this code:

using System.Web.Mvc;
using Microsoft.Practices.ServiceLocation;
using SolrNet;

namespace SOLRTest.Helpers
{
   public static class HtmlHelperMapperExtensions
   {
       private static IReadOnlyMappingManager mapper
       {
           get { return ServiceLocator.Current.GetInstance<IReadOnlyMappingManager>(); }
       }

       public static string SolrFieldPropName<T>(this HtmlHelper helper, string fieldName)
       {
           return mapper.GetFields(typeof(T))[fieldName].Property.Name;
       }
   }
}

And finaly in Global.asax I use this to connect to server:

using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using SOLRTest.Models;
using SolrNet;

namespace SOLRTest
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
           AreaRegistration.RegisterAllAreas();

           WebApiConfig.Register(GlobalConfiguration.Configuration);
           FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
           RouteConfig.RegisterRoutes(RouteTable.Routes);

           Startup.Init<Customer>("http://service...local:8080/solr/msglog_pilot...");
        }
    }
}

Errors which I get is:

Server Error in '/' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0119: 'SOLR4.Models.Customer' is a 'type', which is not valid in the given context

Source Error:


Line 12:     </body>
Line 13: </html>
Line 14:         **<% foreach (var izpis in SOLR4.Models.Customer)**
Line 15:            { %>
Line 16:                <li>

How to write correct code for View in MVC4, and also is my code for simple query in Controller correct.

Please help. Thanks for ideas.

Daniel

Was it helpful?

Solution

<% foreach (var izpis in SOLRTest.Models.Customer)
   { %>
       <li>
           <ul>
                 <%= Html.SolrFieldPropName<Customer>(izpis) %>                 
           </ul>

       </li> 
<% } %>

SOLRTest.Models.Customer is a type, not a variable. You cannot foreach over a Type. Change your foreach to loop over your actual model variable.

You are not returning your model from your controller either.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top