Unfortunately I'm relatively new to MVC so what I am trying to do might be quite simple or not even possible in MVC.
I have a series of template classes in a library which can have template added, change or removed between versions. What I am trying to do is create a page that will allow for the user to select from a drop down list which template they wish to work on and then once they have selected the template be able to populate the properties of the template through text boxes, drop down lists, date selectors etc.
Whilst I could in theory create a View for each of the different templates, I would like to avoid that as if the templates can be added/changed/removed with newer versions of the library, I would like to avoid having to rewrite the Views each time that happens.
Does anyone have any suggestions on how I might be able to achieve this or know of existing references dicussing this?
If I've missed out any information which would be of use please let me know.
Thanks for any help/advice in advance.
Satal
Without writing the code, what you are looking for is a component that builds html based on what model/object it is passed to satisfy the population of that model via a web browser.
I dare say that this is possible!
An easy or easier route would be:
Decorate the properties within your template models with attributes that would dictate what sort of input control they require.
Also decorate the properties with an attribute that specifies the name of the attribute.
Create a component that accepts a model/type and creates HTML to satisfy the input of that type. Of course this must be rendered within a form.
On postback/submit to your action use the Request["{propertyName}"] to get the value of input fields to populate the properties.
Your controller action will accept the name of the Template so that you know which model to create
System.ComponentModel.DataAnnotations is a good place to start before writing your own custom attributes.
Related
I'm trying to create a custom module which includes a content type (Project) which contains a content-part (ProjectPart). Projects will be created by admin. And the goal is to show these created project on a specific page.
When I try to instantiate a new project on the admin menu, I only see one field (Description). Other fields are not shown.
I followed the steps (starting from PersonPart.cs) in "Lombiq.TrainingDemo" project, and managed to create a custom module which includes a content type (Project), a content part (ProjectPart). Comments in the project claim that we can create an instant with all fields. But this is not true for my case.
Here is my code:
https://gist.github.com/vogucore/efb0096e349591c235103e4f3f9e60d1
Here you can see what I exactly mean.
I expect to see & edit all of its field, but only "Description" field is shown up.
Could you please tell me whether I'm missing something, or the explanation in the project is not true?
Thanks in advance. Peace! (;
It seems you are confusing properties of a content part with fields of a content part, which are two different concepts in Orchard.
Parts are regular C# objects that can have regular C# properties. Using such properties is the valid and mainstream way for developers to extend the CMS and build custom parts in Orchard.
Fields are smaller units of contents (typically a single string, or a number, or an image, etc.) that can be dynamically added to a part, trough code or through the admin's content type definition editors. They have the advantage that the administrator of the site can add, remove and modify them at runtime.
It's very understandable that you'd confuse them: they are both name/value thingies that you can attach to a part. In terms of actual usage, a good way to choose between the two is to ask the following question:
Is this a core property that is firmly attached to the part concept I'm building, or something that would be nice for administrators to attach to it if it makes sense to them?
For example, if I build a commerce product part, it would make sense that the SKU and price would be part properties. Now a product photo is different in that not all products may necessarily have a photo, some may have several, may be videos, who knows? It's just not an intrinsic property that makes a product a product. So make that a field (a media picker field to be specific).
To get back to your specific example, you should not be adding that field in your migration: it's basically creating something that looks like it's your Description property, but actually is a Description field, in addition to your property of the same name.
The reason why your part properties don't appear on your editor is that you aren't done with your part. You still need to build a driver, and views for it (the field didn't need that because they were already built by whoever built that field).
As a side note, I think the good Lombiq folks made a lousy choice by giving that Biography property the TextField type. It's very confusing, and should really be a string, in my opinion, and so should your Description property.
I am developing UI for a dynamic platform, a user can add his/her own properties to classes in platform. I will go through an example to make my question clear:
Say we have a class X with ID and DisplayName properties. These are default initial properties of class X. In the ASP.Net web Application I have created an associated form for class X with both properties bound to model. Yet users can add fields to this class and I want to create associated sections in the UI, but with condition that the user can offer correct HTML (or aspx) markup for new field(s) and the markup would render in the UI. This is to avoid changing code for little domain manipulations and does not require code recompile and server restart.
Said Otherwise, I want a functionality like saving partial views in the DB and loading them in the UI.
I have found this Question so close to mine, yet the provided answer is not good enough.
If you can point me to any project, tutorial or sample code, I will appreciate it.
What I have done so far: I have checked Naked Objects for MVC, yet I think it is an overkill for my question. MVC Form Factory is very interesting but I need those added variables be accessible using provided template.
Based on user credentials, I will allow users to edit a field or not on a Razor View.
So I currently have one model which I can do this for the properties I forbid:
[Editable(allowEdit=false)]
public string FirstName {get;set;}
but when I add the attribute whether or not a user has permission to edit it, they won't be able to. I cannot change the fields in Razor View either as we use a very different way of rendering model properties.
Any idea how can I overcome this problem?
You might want to create custom validation attribute and use that, in it you can inject your boolean and check and make it conditional (in C#).
The better option is to use Fluent Validation - its much easier to work with in these cases, when you need conditional validation. Also it will keep your models cleaner.
So if you can - use Fluent validation, if not, just define your own property and control it in your code including all conditions. Examples and links to libraries are here
Here is another option for you - complete library built with aim to easy the work you trying to achieve : http://foolproof.codeplex.com/
Hope this helps
I'm in the need to create a custom field type that renders like a LinkValue field. Instead of forwarding the browser to a different page, I would like to execute JavaScript code (in order to use OpenPopUpPage) depending on several parameters (maybe stored in SPFieldMultiColumnValue?).
What is the best way to approach the requirement?
This has to be the best reference on custom field types from Bjorn Furuknap
Customizing the User Experience of SharePoint: Custom fields deep dive
There is some pain involved in custom fields types though, in his own words
“Looking back I think it would have
been easier, and less painful, to eat
my own eyes.”
With asp.net mvc you can use the annotation
[Required (errormessage="This is required")]
How can I create something like this:
[Required (errormessage="ERRORXX")]
So I can look up in a database what this ERRORXX is and display it on my form. Now my form displays ERRORXX.
How can I create something that solves my problem?
Thx!
Just an idea: why not pull the error messages from a resx file? I think this is the common way of doing this. It even allows you to localize your error messages easily.
I think that by using a resource file (resx file) it's even easier to change the error messages later on. A resx file can be opened and edited in Word Pad for example. You don't need to access a database with username/password, query it, etc.
Localizing ASP.NET MVC Validation
Globalizing ASP.NET MVC Client Validation
Take a look here too:
Model Validation & Metadata in ASP.NET MVC 2
Customizing ASP.NET MVC 2 - Metadata and Validation
The default route to take is with Resources.
However, I understand your pain :) The way I've achieved it is a little unusual, but I'll give you a quick rundown.
In our project, using resource files is not an option as its way too limited for our purposes, the details of which I won't bore you with now! :)
At it's most basic principle, we're setting the errorMessage property of the validation attribute to some sort of "key", and then just using that as a way to lookup the correct (languaged) response in our CMS database, when the validation fails (in our case using MVC, when we update the model and check the state - all at Controller level).
This is the same principle as using the resources (by specifying "ErrorMessageResourceName" and "ErrorMessageResourceType"), but you get to do what you want with it.
To be clear, we originally extended the RequiredAttribute (as one example) with our own stuff, including putting in properly named arguments to allow us to retrieve a sensible CMS value from the database later on. To be extra clear, we're using MVC and custom HtmlHelpers to render our own ValidationControls, which are what ultimately consume the custom values from our custom annotations, etc - None of this affects the dumbed-down principle here though, which is to just use "errorMessage" , or something like it, as a way to look up the actual message from where YOU want to, and WHEN you want to.
I think you mean you want to read/use attribute declarations for a given property?
If so, you could either make your own RequiredAttribute class (to allow adding new or more appropriate properties as you wish). See: Attributes Tutorial