Spread ASP.NET 16
Spread for ASP.NET 16 Product Documentation / Developer's Guide / Getting Started / Working with the Component / Working with Microsoft ASP.NET MVC 5
In This Topic
    Working with Microsoft ASP.NET MVC 5
    In This Topic

    You can use Spread for ASP.NET in an MVC 5 project. MVC support in Spread for ASP.NET requires Microsoft ASP.NET MVC 5, Microsoft Visual Studio 2015 (or later) with .NET 4.6.2 Framework (or later), and the Microsoft ADO.NET Entity 4.1 Framework.

    The Razor view generally uses @ in front of the name and the ASPX view generally uses <% %>around the name. Use the following steps to create a project with Spread:

    1. Reference FarPoint.Mvc.Spread.dll and FarPoint.Web.Spread.dll in the project.
    2. Add the Spread information to the Licenses.licx file:
      Code
      Copy Code
      FarPoint.Web.Spread.FpSpread, FarPoint.Web.Spread, Version=12.45.20182.0, Culture=neutral, PublicKeyToken=327c3516b1b18457
      FarPoint.Mvc.Spread.FpSpread, FarPoint.Mvc.Spread, Version=12.45.20182.0, Culture=neutral, PublicKeyToken=327c3516b1b18457
      
    3. Open Global.asax.cs, go to the Application_Start function and add the following registration code:
      If you use Spread MVC on .NET Framework 4.0 or above, remove FarPoint.Mvc.Spread.MvcSpreadVirtualPathProvider.AppInitialize(); from Application_Start().
      C#
      Copy Code
      protected void Application_Start()
      {
      //FarPoint.Mvc.Spread.MvcSpreadVirtualPathProvider.AppInitialize();
      AreaRegistration.RegisterAllAreas();
      RegisterGlobalFilters(GlobalFilters.Filters);
      RegisterRoutes(RouteTable.Routes);
      //ModelBinders.Binders.DefaultBinder = ModelBinders.Binders[typeof(FarPoint.Mvc.Spread.FpSpread)];
      //ModelBinders.Binders.Add(typeof(FarPoint.Mvc.Spread.FpSpread), new FarPoint.Mvc.Spread.MvcSpreadModelBinder());
      }
      
    4. Declare Spread with the MVC Spread namespace:
      Code
      Copy Code
      @using FarPoint.Mvc.Spread; or <%@ Import Namespace="Farpoint.Mvc.Spread" 
      %>
      

      This allows you to have an MVC Spread with the following code:

      Code
      Copy Code
      @Html.FpSpread("FpSpread1"); or <=Html.FpSpread("FpSpread1")>
      

      FpSpread1 is the Spread ID. It should be unique.

    5. Provide access to MVC Spread from the Controller. When the user posts back data to the server, the developer can access the declared MVC Spread as an argument (the Spread has full ViewState and new postback data). For example:
      C#
      Copy Code
      public ActionResult Index([FarPoint.Mvc.Spread.MvcSpread]FarPoint.Mvc.Spread.FpSpread FpSpread1)
      {
      ViewBag.Message = "Welcome to GrapeCity";
      if (FpSpread1 != null)
      {
      var value = FpSpread1.ActiveSheetView.Cells[0, 0].Value;
      }
      return View();
      }
      

      If you do not want to use an attribute, open the Global.asax.cs and uncomment one of the lines in step 3.

      Make sure the Spread ID is the same in the view code and in the controller action parameter.

    6. Attach Spread events:

      Spread supports attaching events from the Controller only. If there is an AJAX postback, the Spread events will not be handled. MvcSpread allows attaching 3 main events: Init, Load, and PreRender. Events can be grouped or ungrouped. Use one of the following methods to handle the event:

      Create a function with a special name.

      The special name indicates that “I want to bind this function to a Spread event”. For example, to attach to the Load event of FpSpread1, the function looks like the following:

      C#
      Copy Code
      public void FpSpread1_Load(object sender, EventArgs e)
      {
      }
      Use MvcSpreadEventAttribute.
      In some cases, you may want to reserve a special name (like “FpSpread1_Load”). This can only be done by using the second method: MvcSpreadEventAttribute. The event handler can be shared globally or in a group.
      This example handles the Init event for all FpSpreads with the ID of FpSpread1 in any view, globally:
      [FarPoint.Mvc.Spread.MvcSpreadEvent("Init", "FpSpread1")]
      private void _init(object sender, EventArgs e)
      {
      }
      

      This solution also provides the ability to bind one function to many different Spreads. The following example handles the Init event for all FpSpreads with the ID of FpSpread1 or FpSpread2 in any view, globally:

      C#
      Copy Code
      [FarPoint.Mvc.Spread.MvcSpreadEvent("Init", new string[] {"FpSpread1", "FpSpread2"})]
      private void _init(object sender, EventArgs e)
      {
      }
      

      The second solution requires that you indicate implicitly that the function (with special name) should not be attached automatically:

      C#
      Copy Code
      [FarPoint.Mvc.Spread.NoMvcSpreadEvent]
      private void FpSpread1_Init(object sender, EventArgs e)
      {
      }
      

      By attaching to the Init or Load event, you can attach to other custom Spread events such as TopRowChange, UpdateCommand, and so on.

      Additional information about global, grouped, and ungrouped events:

      Attaching events with a grouped MvcSpread:

      If the MvcSpread is grouped, use a group name. For example:

      C#
      Copy Code
      // groupName is "GroupName" -> grouped
      public ActionResult Index([MvcSpread("GroupName", false)] FpSpread FpSpread1)
      // no groupName specified -> not grouped
      public ActionResult Index([MvcSpread(false)] FpSpread FpSpread1)
      

      The following examples show how to handle group events:

      C#
      Copy Code
      // method Func3() is used to handle the Load event for all FpSpreads with ID of FpSpread1 in any view, inside the group called "GroupName"
      [MvcSpreadEvent("Load", "GroupName", "FpSpread1")]
      private void Func3(object sender, EventArgs e)
      {
      }
      // method Func4() is used to handle the Load event for all FpSpreads with ID of FpSpread1, or FpSpread2 in any view, inside the group called "GroupName"
      [MvcSpreadEvent("Load", "GroupName", new string[] { "FpSpread1", "FpSpread2" })]
      private void Func4(object sender, EventArgs e)
      {
      }
      

      Any function named “[Spread ID]_[Event Name]” is treated as a global event handler. The event name is indicated by [Event Name] and only an MvcSpread with an ID the same as [Spread ID] can handle this event handler:

      C#
      Copy Code
      // this is a global handler for all FpSpread1 Load events
      public void FpSpread1_Load(object sender, EventArgs e)
      {
      }
      

      You can exclude some groups (that contain FpSpread controls that are not handled by this method) from global event handlers. These groups can be referenced by their names. For example:

      C#
      Copy Code
      // method abc() is used to handle the Load event for all FpSpread1 controls, excluding the ones that belong to GroupName1 or GroupName2
      [MvcSpreadEvent("Load", "FpSpread1")]
      [MvcSpreadEventExclude("GroupName1", "GroupName2")]
      public void abc(object sender, EventArgs e)
      {
      }
      // this method is used to handle the Load event for all FpSpread1 controls, excluding the ones that belong to GroupName1 or GroupName2
      [MvcSpreadEventExclude("GroupName1", "GroupName2")]
      public void FpSpread1_Load(object sender, EventArgs e)
      {
      }
      

      Developer does not declare MvcSpread inside controller:

      The MvcSpread is ungrouped if the MvcSpread is declared inside the view. Using a group name with MvcSpread in the view may cause issues since there is a case where the group name in the controller and the group name in the view are different. If MvcSpread is not declared in the controller, the events are attached to global event handlers only.

      Developer declares MvcSpread inside controller:

      If the event handlers are global (group name not declared in MvcSpreadEventAttribute), they attach to all Spread controls whether grouped or not.

      If the event handlers are private (group name explicitly declared in MvcSpreadEventAttribute), they attach only to Spread controls with the same group name.

      Spread controls without a group name are attached by global event handlers.

      If there are two event handlers for the same event, the first one is private and the second one is global.

      The current Spread looks for the global event handler first when binding events. If found, binding for the current event happens first. The private one with the same group name happens next.

      Note: Using the create parameter to notify MvcSpreadModelBinder to create a new instance of MvcSpread the first time does not affect attaching events.An event handler is attached to MvcSpread if the event ID list contains the MvcSpread ID, regardless of whether the event handler is private or global.

    7. Pass MVC Spread from Controller to View:

      If an instance of FpSpread is created by a model, it is applied to the view automatically. The code that renders FpSpread with the same ID, renders the current FpSpread to the client browser:

      C#
      Copy Code
      public ActionResult Index([MvcSpread(true)] FpSpread FpSpread1)
      {
      FpSpread1.ActiveSheetView.Rows.Count = 30;
      return View();
      }
      

      In the parameter list Index() action the FpSpread1 is declared specifically as [MvcSpread(true)] FpSpread FpSpread1. When running this code, a new instance of FpSpread is created by our ModelBinder. The ActiveSheetView.Rows.Count is set to 30, and then this instance is applied to the view automatically. The FpSpread control with the ID of "FpSpread1" in the view receives these changes.