How to override the built in templates in MVC Core similar in MVC 4 - asp.net-core-mvc

in ASP MVC (before MVC core like mvc 4,5 ) , we can override the built in templates like string , boolean from EditorTemplates/Boolean.ascx ,the question is :
Is there any way to do it in MVC Core similar to this article https://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html which is applied to MVC 2 ?

It still works the same way, though it was never .ascx. These are Razor views, so they'll need a .cshtml extension. Assuming you add a view like Views\Shared\EditorTemplates\Boolean.cshtml, then all you need to do is #Html.EditorFor(m => m.MyBooleanProp).

Related

Are there breaking changes in model binding of collections in MVC4?

Ok, so after migrating from MVC3 to MVC4 I ran into interesting problem.
We had a controller method with following signature:
public ActionResult Delete(Guid[] items)
This worked great in MVC3 but stopped working after upgrade to MVC4 - the items array is always empty. I realized that in MVC4 I would have to use ICollection instead to make it work:
public ActionResult Delete(ICollection<Guid> items)
Is that a breaking change? Or the first approach is not really a proper way of doing things that just worked as a fluke in MVC3?
The data that is posted looks very simple and looks like this:
items=52b37b94-1f53-4981-a698-9eb6eca30861&items=d2f8c5e5-4e04-4a97-8efd-643a4e87e48b
And is posted using jQuery ($.post)
Ok, I figured out what was happening. I started brand new MVC4 project to create a vanilla test case, and binding worked properly for both array and collection of Guids.
After digging I realized that during upgrade we also upgraded autofac DI container from version 2.5 to 3.0. Some internals may have changed and action injection got enabled by default.
Apparently action injection was interfering with model binding.

RDLC reports with ASP.net MVC 3.0

I'm trying to add business object data source for RDLC report in my MVC 3.0 application.However, it couldn't allow me to add object data source to my application.
How I add object data source to my RDLC report ?
Just add an aspx page with all the functionalities you need as you would in a standard WebForms application. You will just need to add a route to it. Here is an example that will probably suite your needs:
public static void RegisterRoutes(RouteCollection routes)
{
// ...
// Some MVC routes
// ...
//Custom route for reports
routes.MapPageRoute(
"ReportRoute", // Route name
"Reports/{reportname}", // URL
"~/Reports/{reportname}.aspx" // File
);
}
The ReportViewer component is not intended to be used with ASP.NET MVC and is unsupported. You may checkout this example. You could have a standard WebForms page (not MVC) generate the report and then include it either with iframe inside your MVC application or have it generate the report as PDF which then could be embedded.
Then check this blog:
asp-net-mvc-handling-ssrs-reports
Handling SSRS Report in ASP.Net MVC 3
Hope this helps.
Regards

From web forms to Razor

I've got an .net 2.0 web forms site that has just been upgraded to .net 4. Now I'd like to use the Razor syntax and some mvc helpers. Could anyone give a step by step procedure to start using it?
(Yes, I know mixing different view engines is not straight forward, but I'm not asking for that. Just to be able to create a new _layout, and a new ContentPage.cshtml and start using some of the mvc helpers and get that to work in parallell with the old pages - I'll duplicate the masterpage functionality, so that new pages will be written using razor, and old pages bugfixed in webform with the old masterpage)
I just need to know the following:
What assemblies do I need to include
What changes to web.config do I need
Any other changes?
Thanks for any help
Larsi
Scott hanselman has a great post about this:
Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications
You need to include System.Web.Mvc version 3.0.
In your web.config, you need to make sure that the UrlRoutingModule is registered as an HttpModule. Your IHttpHandler is created by the IRouteHandler implementation, which is an MvcRouteHandler in ASP.NET Mvc.
You also will need to register your routes in your Global.asax to setup routing. The default Route registration (for an MVC2 project) looks like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
I'm not sure if they have made any changes to that in Mvc 3 or not, but you can find out by creating a new Mvc Web Application project in Visual Studio and opening up the Global.asax
You may take a look at the upgrading an ASP.NET MVC 2 Project to ASP.NET MVC 3 guide. If you have a classic WebForms application (not MVC) then there is no migration => there is a rewrite.
This converter tool will get you a head start:
http://visualstudiogallery.msdn.microsoft.com/d2bfd1ca-9808-417c-b963-eb1ea4896790
Telerik wrote a command-line converter from aspx/ascx to cshtml for asp.net mvc. You can find that at: https://github.com/telerik/razor-converter
There is also a nice plugin for Visual Studio that uses the Telerik code at: http://visualstudiogallery.msdn.microsoft.com/d2bfd1ca-9808-417c-b963-eb1ea4896790

Why is CompareAttribute in the MVC namespace and not the DataAnnotations namspace?

MVC 3 includes a new validation attribute called CompareAttribute.
But why is this validation attribute in the mvc namespace and not with all of the other validation attributes in the DataAnnotations namespace?
Are the other validation attributes spread throughout other namespaces?
It's just the way things worked out. Derived types do not have to be in the same namespace as the base type. DataAnnotations is part of the .NET framework which releases a lot less frequently than a standalone project like ASP.NET MVC.
It's possible that there are types derived from ValidationAttribute in other namespaces, but most are part of the core DataAnnotations.

In ASP.NET MVC 3, what class does Views and Partial Views inherit from?

In ASP.NET MVC 3, what class doe Views and Partial Views inherit from and can the class (presumably it's a class somewhere) be inherited from to extend functionality.
For example, the default LogOnPartial control has the following (using Razor syntax)
Welcome <b>#Context.User.Identity.Name</b>!
Where is the Context object exposed. What class makes that available to the partial view?
They inherit from System.Web.Mvc.WebViewPage. http://msdn.microsoft.com/en-us/library/system.web.mvc.webviewpage%28VS.98%29.aspx In older versions of razor it was required to add the #inherits but newer versions don't.

Resources