How to use Metadata validation in blazor? - c#

I have a project include two project in same Solution Explorer
First: DbModelContextProvider (Class library)
Second: AppApiProvider (MVC core api -v3.1.4)
In DbModelContextProvider project i want create Metadata validation for Department
public class DepartmentMetaData
{
[Display(Name = "Department Name")]
[Required]
public string Name { get; set; }
}
Then create new class Department but partial class
[MetadataType( typeof(DepartmentMetaData))]
public partial class Department
{
}
Same code use with MVC core web app work success but when i want work
with Blazor not work

It seems it is not yet supported for Blazor. https://github.com/dotnet/aspnetcore/issues/20805

Related

ASP.NET Core MVC add/remove dynamic content

I'm currently developing a system which manages work times and analyze them. Therefore I need to add several Time-Stamps once and add/remove some input field dynamically.
The ViewModel looks like:
public class CreateWorkDayViewModel
{
public DateTime Date { get; set; }
public IEnumerable<CreateStampModel> Stamps { get; set; }
}
public class CreateStampModel
{
public string ProjectId { get; set; }
public DateTime From { get; set; }
public DateTime To { get; set; }
}
How to do this with the razer syntax including validation?
I'm developing with Visual Studio Version 15.2 and the latest stable MVC version.
EDIT (17.05.17):
What I've done so far:
I added a jQuery based mechanism to add/remove rows dynamically.
Click for the mechanism
My problem is, that in the part of where I'm adding the rows(at JS line 4) I can't use the razor syntax because this html code is inserted at runtime by jQuery.
I also tried it with the old way and used instead of asp-for name. This works for the post data, but to fill the project drop-down selection I need the data from the ViewModel and this is missing.

Generating Typescript Definition from partial classes using Web Essentials

I'm trying to get my typescript definition files generated for my database first entity framework classes using web essentials 2015 with VS 2015.
I followed the example here to generate my .net classes the new way, and i have POCOs for all the tables. e.g.
//module User.cs
public partial class User
{
public int Id { get; set; }
public string User { get; set; }
public DateTime DateOfBirth { get; set; }
}
Usually, i will extend these will some partial classes so i can add some functionality outside of the database, i wouldn't modify my generated code in case i need to regenerate it later.
//module SiteModels.cs
public partial class User
{
public double DaysOld { get { return (DateTime.Now - DateOfBirth).TotalDays ; } }
}
But web essentials only looks at the code in the file that i am generating a typescript intellisense file for, so in my typescript definition i only get:
//generated module SiteModels.cs.d.ts
interface User {
daysOld: number;
}
So the crux of the problem is: when i have 2 partial class in 2 different files, and i am using web essentials to convert one to a typescript definition file, it's not including all of the properties from both partial classes.
Suggestions for alternate routes are acceptable, but they need to work with ASP NET Core.

Reuse Model from another project

I've got a Custom Validator Attribute called RequiredIf inside a Model in my main Web Project and I need to reuse that model in another Model from another project. When I look at the disassembled dll from the Main Project inside the subproject, the Custom Attribute just disappears and the Custom Attribute doesn´t work.
Here goes my structure:
MainProject
[RequiredIf( "TipoReagendamentoSco", false, "CodigoTipoReagendamento", null, "Tipo Reagendamento é obrigatório")]
[Display(Name = "Tipo Reagendamento:")]
public int CodigoTipoReagendamento { get; set; }
SubProject
public TipoReagendamentoModel TipoReagendamento { get; set; }
Disassembled MainProject.DLL
[Display(Name = "Tipo Reagendamento:")]
public int CodigoTipoReagendamento { get; set; }
The Custom Validator Attribute (RequiredIf) went away. But when I use 'Required' from DataAnnotations (not a custom attribute), it works like a charm.
Thanks !

Is there an attribute that I can use with ASP.NET MVC 3 to prevent model fields from being automatically included in my views?

Is there an attribute that I can use with ASP.NET MVC 3 to prevent model fields from automatically showing up in my view? What I mean by this is that I have classes like the following:
public class EntityBase
{
public int ID { get; set; }
//more fields...
}
public class TestEntity : EntityBase
{
public string TestEntityName { get; set; }
//more fields...
}
I know about all of the attributes in System.ComponentModel and System.ComponentModel.DataAnnotations to enforce validation - Required, StringLength, etc. - but is there one I can use that will prevent certain fields from showing up in the view when I create it from Visual Studio? All of my project's model classes inherit from EntityBase, but I don't want any of EntityBase's fields to be visible on the view. I'm using Razor as my ViewEngine, in case it matters.
TIA,
Benjy
Use ScaffoldColumn:
[ScaffoldColumn(false)]

Silverlight/Web Service Serializing Interface for use Client Side

I have a Silverlight solution that references a third-party web service. This web service generates XML, which is then processed into objects for use in Silverlight binding. At one point we the processing of XML to objects was done client-side, but we ran into performance issues and decided to move this processing to the proxies in the hosting web project to improve performance (which it did). This is obviously a gross over-simplification, but should work. My basic project structure looks like this.
Solution
Solution.Web - Holds the web page
that hosts Silverlight as well as
proxies that access web services and
processes as required and obviously
the references to those web
services).
Solution.Infrastructure - Holds
references to the proxy web services
in the .Web project, all genned code
from serialized objects from those
proxies and code around those objects
that need to be client-side.
Solution.Book - The particular
project that uses the objects in
question after processed down into
Infrastructure.
I've defined the following Interface and Class in the Web project. They represent the type of objects that the XML from the original third-party gets transformed into and since this is the only project in the Silverlight app that is actually server-side, that was the place to define and use them.
//Doesn't get much simpler than this.
public interface INavigable
{
string Description { get; set; }
}
//Very simple class too
public class IndexEntry : INavigable
{
public List<IndexCM> CMItems { get; set; }
public string CPTCode { get; set; }
public string DefinitionOfAbbreviations { get; set; }
public string Description { get; set; }
public string EtiologyCode { get; set; }
public bool HighScore { get; set; }
public IndexToTabularCommandArguments IndexToTabularCommandArgument { get; set; }
public bool IsExpanded { get; set; }
public string ManifestationCode { get; set; }
public string MorphologyCode { get; set; }
public List<TextItem> NonEssentialModifiersAndQualifyingText { get; set; }
public string OtherItalics { get; set; }
public IndexEntry Parent { get; set; }
public int Score { get; set; }
public string SeeAlsoReference { get; set; }
public string SeeReference { get; set; }
public List<IndexEntry> SubEntries { get; set; }
public int Words { get; set; }
}
Again; both of these items are defined in the Web project. Notice that IndexEntry implments INavigable. When the code for IndexEntry is auto-genned in the Infrastructure project, the definition of the class does not include the implmentation of INavigable. After discovering this, I thought "no problem, I'll create another partial class file reiterating the implmentation". Unfortunately (I'm guessing because it isn't being serialized), that interface isn't recognized in the Infrastructure project, so I can't simply do that. Here's where it gets really weird. The BOOK project CAN see the INavigable interface. In fact I use it in Book, though Book has no reference to the Web Service in the Web project where the thing is define, though Infrastructure does. Just as a test, I linked to the INavigable source file from indside the Infrastructure project. That allowed me to reference it in that project and compile, but causes havoc in the Book project, because now there's a conflick between the one define in Infrastructure and the one defined in the Web project's web service. This is behavior I would expect.
So, to try and sum up a bit. Web project has a web service that process data from a third-party service and has a class and interface defined in it. The class implements the interface. The Infrastructure project references the web service in the Web Project and the Book project references the Infrastructure project. The implmentation of the interface in the class does NOT serialize down, so the auto-genned code in INfrastructure does not show this relationship, breaking code further down-stream. The Book project, whihc is further down-stream CAN see the interface as defined in the Web Project, even though its only reference is through the Infrastructure project; whihc CAN'T see it.
Am I simple missing something easy here? Can I apply an attribute to either the Interface definition or to the its implmentation in the class to ensure its visibility downstream? Anything else I can do here?
I know this is a bit convoluted and anyone still with me here, thanks for your patience and any advice you might have.
Cheers,
Steve

Categories

Resources