MVC
No edit summary
(Automatically adding template at the end of the page.)
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
The Turnkey WebApplication uses MVC for index and login pages - it defaults to Angularjs for all other pages. To get MVC on other pages, you must set the tagged value MVC=true on ViewModel.
The Turnkey WebApplication uses MVC for index and login pages. It defaults to AngularJS for all other pages. To get MVC on other pages, you must set the tagged value <code>MVC=true</code> on a ViewModel.


MVC works differently from Angular in a number of ways. The main differences are these:
MVC works differently from Angular in several ways. The main differences are these:
* Grid-row-clicks, in MVC select of row, is done if there are zero or many actions, but if there is 1 action (defined in ViewModel) this action is executed on row click
# Grid-row-clicks, in MVC select of row, is done if there are zero or many actions, but if there is 1 action (defined in ViewModel) this action is executed on row click
* MVC never shows a popup menu of multiple actions as Angular does
# MVC never shows a popup menu of multiple actions as Angular does


==== How to Build MVC Apps With MDrivenFramework ====
==== How to Build MVC Apps With MDrivenFramework ====
[[MVC_Generated_ViewModel_UI_in_MDrivenFramework]]
* [[MVC_Generated_ViewModel_UI_in_MDrivenFramework]]
 
* [[Comboboxes_in_MVC_from_model_driven_ViewModel]]
[[Comboboxes_in_MVC_from_model_driven_ViewModel]]
* [[MVC_View_Model_handling]]
 
* [[Getting_started_template_for_MDriven_MVC]]
[[MVC_View_Model_handling]]
 
[[Getting_started_template_for_MDriven_MVC]]
 
[[MVC_White_paper_in_4_parts]]


=== New MVC Notes Autumn 2021 ===
=== New MVC Notes Autumn 2021 ===
Line 21: Line 16:
To get a package into an EcoSpace by code, add an attribute to EcoSpace like [EcoSpacePackage(typeof(Package1Package))]
To get a package into an EcoSpace by code, add an attribute to EcoSpace like [EcoSpacePackage(typeof(Package1Package))]


To get binding a codegen ViewModel to work, do this:
To get binding a Codegen ViewModel to work, do this:
  public IActionResult View([ModelBinder(typeof(VMClassBinder))] SampleViewModel value)
  public IActionResult View([ModelBinder(typeof(VMClassBinder))] SampleViewModel value)
  {
  {
Line 31: Line 26:
   options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(VMClass)));
   options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(VMClass)));
  }); // to avoid validation of the complete world by following VMClass refs
  }); // to avoid validation of the complete world by following VMClass refs
To enable your page to roundtrip truly stateless we need to know the ViewModel name and Root object id on postback. Add these hidden fields in your form:
To enable your page to roundtrip truly stateless, you need to know the ViewModel name and Root object ID on postback. Add these hidden fields in your form:
  <input asp-for="ThisAsExternalId" hidden />
  <input asp-for="ThisAsExternalId" hidden />
  <input asp-for="ViewModelClass.ViewModel.Name" hidden/>
  <input asp-for="ViewModelClass.ViewModel.Name" hidden/>
To use Runtime generated Razor files from ViewModel do like this - see comments on why each row is added and choose wisely:
To use Runtime-generated Razor files from ViewModel, try this (see comments on why each row is added and choose wisely):
<pre>
<pre>
public class Startup
public class Startup
Line 102: Line 97:
   }
   }
</pre>
</pre>
Setup EcoSpace and add model package references in .net5 go like this:
Setup EcoSpace and add model package references in .net5 like this:
<pre>
<pre>
namespace ClassLibrary6
namespace ClassLibrary6
Line 122: Line 117:
     }
     }


....</pre>The example for download
....</pre>The example for download: [[File:2021MVCNet5.zip|ZipFile|thumb]]
[[File:2021MVCNet5.zip|ZipFile|thumb]]


Video discussing the example: https://youtu.be/UAA4_9fRNbA
Video discussing the example: https://youtu.be/UAA4_9fRNbA
[[Category:MVC]]
[[Category:MVC]]
{{Edited|July|12|2024}}

Revision as of 15:37, 10 February 2024

The Turnkey WebApplication uses MVC for index and login pages. It defaults to AngularJS for all other pages. To get MVC on other pages, you must set the tagged value MVC=true on a ViewModel.

MVC works differently from Angular in several ways. The main differences are these:

  1. Grid-row-clicks, in MVC select of row, is done if there are zero or many actions, but if there is 1 action (defined in ViewModel) this action is executed on row click
  2. MVC never shows a popup menu of multiple actions as Angular does

How to Build MVC Apps With MDrivenFramework

New MVC Notes Autumn 2021

New Nuget packages for .net5 and .netStandard are published on Nuget and named MDriven.*

To get a package into an EcoSpace by code, add an attribute to EcoSpace like [EcoSpacePackage(typeof(Package1Package))]

To get binding a Codegen ViewModel to work, do this:

public IActionResult View([ModelBinder(typeof(VMClassBinder))] SampleViewModel value)
{
  return View("View");
}

To suppress the bogus validation done on VMClass, do this:

var mvc = services.AddMvc(options =>
{
  options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(VMClass)));
}); // to avoid validation of the complete world by following VMClass refs

To enable your page to roundtrip truly stateless, you need to know the ViewModel name and Root object ID on postback. Add these hidden fields in your form:

<input asp-for="ThisAsExternalId" hidden />
<input asp-for="ViewModelClass.ViewModel.Name" hidden/>

To use Runtime-generated Razor files from ViewModel, try this (see comments on why each row is added and choose wisely):

public class Startup
  {
    public Startup(IWebHostEnvironment env, IConfiguration configuration) //added to inject the IWebHostEnvironment, needed for files
    {
      _HostingEnvironment = env;
      Configuration = configuration;
    }

    private IWebHostEnvironment _HostingEnvironment;


    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
      services.AddControllersWithViews();
      services.AddMemoryCache(); // to hold shelved ecospaces
      var mvc = services.AddMvc(options =>
      { 
        options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(VMClass))); // to avoid validation of the complete world by following VMClass refs
      }); 
      mvc.AddSessionStateTempDataProvider();
      services.AddSession(); // To hold selection etc during roundtrip

      services.Configure<MvcRazorRuntimeCompilationOptions>(options => {   // Needed to generate Razor for ViewModels
        options.FileProviders.Clear();
        options.FileProviders.Add(new MDrivenMVCCoreFileProvider(this._HostingEnvironment.ContentRootFileProvider, Directory.GetCurrentDirectory()));
      });

      services.AddRazorPages().AddRazorRuntimeCompilation(); //without AddRazorRuntimeCompilation our fileProvider is not called;

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
      if (env.IsDevelopment())
      {
        app.UseDeveloperExceptionPage();
      }
      else
      {
        app.UseExceptionHandler("/Home/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
      }
      app.UseHttpsRedirection();
      app.UseStaticFiles();
      app.UseSession(); // to hold vm vars during MVC roundtrip

      app.UseRouting();

      app.UseAuthorization();



      app.UseEndpoints(endpoints =>
      {
        endpoints.MapControllerRoute(
                  name: "default",
                  pattern: "{controller=Home}/{action=Index}/{id?}");
      });
    }
  }

Setup EcoSpace and add model package references in .net5 like this:

namespace ClassLibrary6
{
  [EcoSpace]
  [UmlTaggedValue("Eco.InitializeNullableStringsToNull", "true")]
  [UmlTaggedValue("Eco.GenerateMultiplicityConstraints", "true")]
  [EcoSpacePackage(typeof(Package1Package))] // Add packages this way
  public partial class EcoSpace1 : Eco.Handles.DefaultEcoSpace
  {

    #region Eco Managed code
    private static ITypeSystemService typeSystemProvider;
    #endregion Eco Managed code

    public EcoSpace1() : base()
    {
      this.PersistenceMapper = new PersistenceMapperXml() { FileName = @"c:\temp\MVCTempData.xml" }; // simple setup during dev
    }

....

The example for download: File:2021MVCNet5.zip

Video discussing the example: https://youtu.be/UAA4_9fRNbA

This page was edited 9 days ago on 05/08/2024. What links here