I am new to Umbraco CMS. I am using Ui-O-Matic plugin in my project.
Ui-O-Matic allows easy CRUD operation for database. But I want to use backoffice controls like file, textarea, etc.
So I am using UIOMaticFielld like this in database.cs file.
[Column("newsDetail")]
[UIOMaticField("News Detail","Add Details",View ="textarea")]
public string newsDetail { get; set; }
[Column("newsImage")]
[UIOMaticField("Image","Upload Image",View ="file")]
public string newsImage { get; set; }
Problem is when I make any change in database, I have to refresh database.tt file to get database changes. But it recreates database.cs file and my previous changes:
[UIOMaticField("News Detail","Add Details",View ="textarea")]
removes from database.cs file. And every time I have to do the same changes.
Please guide me what should I do to keep my custom changes as it is even I refresh database.tt file?
Other better way to do CRUD operation is also preferable.
After googling a lot, I found that as database.cs file is auto generated, I must not do custom changes in it.
I found another way to use backoffice controls. Let me explain here, may it will help to other.
Instead of writing UIOMatoicField in databse.cs file, create model to do the same.
Make below changes in "Models/Generated/database.tt" file
// Settings
ConnectionStringName = "umbracoDbDSN"; // Uses last connection string in config if not specified
Namespace = "Generator";
RepoName = "";
GeneratePocos = true;
ClassPrefix = "";
ClassSuffix = "";
// Read schema
var tables = LoadTables();
tables["Course"].Ignore = true; // Prevents table to include in databse.cs file
tables["News"].Ignore = true;
if (tables.Count>0)
{
#>
<## include file="UIOMatic.Generator.ttinclude" #>
<# } #>
Then create new Model as below. For ex. "Models\NewsModel.cs"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using UIOMatic.Attributes;
using UIOMatic.Enums;
using UIOMatic.Interfaces;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.DatabaseAnnotations;
namespace EdumentUIOMatic.Models
{
[Umbraco.Core.Persistence.TableName("News")]
[PrimaryKey("newsId")]
[UIOMatic("News", "icon-box-open", "icon-box-open", RenderType = UIOMaticRenderType.List, ConnectionStringName = "umbracoDbDSN")]
public class NewsModel: IUIOMaticModel
{
[UIOMaticIgnoreField]
[Column("newsId")]
public int newsId { get; set; }
[Column("newsTitle")]
[UIOMaticField("News Title", "Add Title")]
public string newsTitle { get; set; }
[Column("newsDetail")]
[UIOMaticField("News Detail", "Add Details", View = "textarea")]
public string newsDetail { get; set; }
[Column("newsImage")]
[UIOMaticField("Image", "Upload Image", View = "file")]
public string newsImage { get; set; }
[Column("isDeleted")]
[UIOMaticField("Hide News", "Check if you want to hide this news")]
public bool isDeleted { get; set; }
[System.Web.Http.AcceptVerbs("GET", "POST")]
public IEnumerable<Exception> Validate()
{
return new List<Exception>();
}
}
}
Now if you will reload database.tt file to get updated database, your code will not be removed.
Related
I am recently starting to learn mobile development for my Final Year Project, and I am doing an Android application using Xamarin.Android and Azure Mobile Services.
I have created a Test DB and created one entry there, trying to make my Android App to connect to the DB and retrieve this entry. I am doing this to get an idea how to establish a connection and retrieve data, and from there I will start modification properly.
That is how my Model class look like (note that JSON properties are named exactly like the columns names in my DB.
using Newtonsoft.Json;
namespace com.parkkl.intro.Models
{
class TestTable
{
public int Id { get; set; }
[JsonProperty(PropertyName = "UserName")]
public string UserName { get; set; }
[JsonProperty(PropertyName = "deleted")]
public bool Deleted { get; set; }
[JsonProperty(PropertyName = "version")]
public string Version { get; set; }
[JsonProperty(PropertyName = "createdAt")]
public string Creation { get; set; }
[JsonProperty(PropertyName = "updatedAt")]
public string Updated { get; set; }
}
}
And that is how my activity look like
using Android.App;
using Microsoft.WindowsAzure.MobileServices;
using Android.OS;
using com.parkkl.intro.Models;
using System.Collections.Generic;
using Android.Widget;
namespace com.parkkl.intro
{
[Activity(Label = "ParkKL", MainLauncher = false, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
#region Variables
public static MobileServiceClient client = new MobileServiceClient
(#"http://parkkl.azurewebsites.net/");
private IMobileServiceTable<TestTable> test = null;
private List<TestTable> testItems;
#endregion
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
test = client.GetTable<TestTable>();
testItems = new List<TestTable>();
GetData();
}
public async void GetData()
{
var collection = await test.Where(user => user.Id == 1).ToCollectionAsync();
foreach (var item in collection)
{
testItems.Add(
new TestTable
{
Id = item.Id,
UserName = item.UserName,
Deleted = item.Deleted,
Version = item.Version,
Creation = item.Creation,
Updated = item.Updated,
});
}
var finalItem = collection[0];
TextView text = (TextView)FindViewById(Resource.Id.TextFromDB);
text.Append(finalItem.UserName);
}
}
}
Now the issue is, every time I try to deploy the app, it throws this exception
Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException:
You must be logged in to use this application
In my App Service in Azure, I have disabled all authentications, still I get this error. I am wondering where the issue is coming from?! I will much appreciate your help.
EDIT: I guess I found the issue, which was the permissions given on the table itself. However, I am still finding out a way to authenticate my app properly
Check out the book http://aka.ms/zumobook for details on how to deal with authentication.
Hi i am started working on umbraco CMS can anybody tell me how to display contents from model to controller and return data to respective view, want to do something like this :-
ContactViewModel.cs
public int id { get; set; }
public string Name { get; set; }
public string address { get; set; }
public string Email { get; set; }
public string phone { get; set; }
ContactController.cs
var result = new ContactViewModel()
{
id = 1,
address = "ghivto",
Email = "nimesh#gmail.com",
Name = "Nimesh khatri",
phone = "9898989898"
};
return View("contactsDemo",result);
how do i list above data to particular view..? i had already tried "https://www.youtube.com/watch?v=sDQwu_DzYyc" but still i didn't get...can anyone elaborate it..thanks in advance..
SurfaceController exposes a controller like you would use in standard MVC and also gives access to the Umbraco helpers.
public MyController : SurfaceController {
[ChildActionOnly]
public PartialViewResult MyAction(SomeObject data)
{
var result = new ContactViewModel()
{
id = 1,
address = "ghivto",
Email = "nimesh#gmail.com",
Name = "Nimesh khatri",
phone = "9898989898"
};
return View("~/Views/PartialViews/contactsDemo.cshtml",result);
}
}
You can access this data from a Template's razor view or a Macro partial by using:
#Html.Action("MyAction","MyController", new { data = new { Test = "I am data" } })
Note: The data passing can be anything, from a single string to full object which binds to variables at the action.
As with MVC there a few ways to access a partial view file. The above example has an "absolute" path, which you could also match your view folder structure to the name of the controller and the view name to the action name. (MyAction.cshtml).
If you want to do that in Umbraco, and still have access to the Umbraco tempting system etc, you need to use a SurfaceController. This is a controller that inherits all of the useful Umbraco Stuff.
Basically, you'd create your ViewModel, and then a Surface Controller to handle all the actions (Render, Process etc). You'd then call that Surface Controller on your Umbraco View for the Contact Page (or whichever page you want this to show on).
It looks like the video you linked to already covers how to do this, but you can find an example that's pretty similar to your example here: http://creativewebspecialist.co.uk/2013/07/22/umbraco-mvc-what-on-earth-is-a-surface-controller/
I am making my project in MVC4, where i am using my Code first approach. i need to update my model
i have a following property which needs to be update , how can i achieve this
public class ContactForm
{
public char Phone { get; set; }
}
public class ConContext : DbContext
{
public DbSet<ContactForm> ContactForms { get; set; }
}
}
i want to update Phone propery to
public char Phone { get; set; }
thnx in advance, i have installed migrations to my projet already
My configuration.cs
namespace MyCRM.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<MyCRM.Models.ConContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(MyCRM.Models.ConContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}
}
The normal flow with EF code-first is too first update the model (a C# file) :
public class ContactForm
{
public string Phone { get; set; } //previously, this was let's say of type int
}
Then, you build your project and after that in the Package Manager Console, you have to call Add-Migration with some label (in order to rollback changes later if needed) :
Add-Migration Phone
This will add to your solution a file named like this 201409xxxxxxxx_Phone under the directory Migrations.
Then you have to put the changes to your database which can be done with the command (always in the console) :
Update-Database
Then, you should be done : the property Phone is of type string everywhere.
I just followed an MVC tutorial on creating an image Gallery, which connects the Controller to the data connection, like this:
ImageController.cs:
...
private CustomMembershipDB db = new CustomMembershipDB();
public ViewResult Index()
{
return View(db.lm_pics.ToList());
}
...
Instead of connecting directly to CustomMembershipDB, I'd like to use my own Model named GalleryModel.cs. I'm thinking this would allow me to create more functionality that just direct data access.
I am not sure how to write this model, or how to reference it in the controller so that it behaves the same way as a direct database connection does now.
Currently, my GalleryModel.cs file looks lke this (edited to correct error):
namespace LMProj_MVC.Models
{
public class GalleryModel
{
public string Picname { get; set; }
public string Decription{ get; set; }
public int Userid { get; set; }
}
public class PicDBContext : CustomMembershipDB
{
public DbSet<GalleryModel> GalleryModel { get; set; }
}
}
I'd like to be able to show the gallery using an iEnumberable list as I am doing now, in addition to creating other methods. Could someone tell me what I'm missing?
You need to create an instance of your model object for each of your database pictures. You could use LINQ to do this, for example:
var picSummaries = db.lm_pics.Select(pic => new GalleryModel{
Picname = pic.Name,
Description = pic.Description,
Userid = pic.User.Id
});
Or you could use a for each loop:
var picSummaries = new List<GalleryModel>();
foreach (var pic in db.lm_pics)
{
picSummaries.Add(new GalleryModel{
Picname = pic.Name,
Description = pic.Description,
Userid = pic.User.Id
});
}
then return the view as before:
return View(picSummaries);
Perhaps there is an easy solution for my problem but I simply cannot seem to find it. I have read lots of tutorials about Knockout so I get the basics but I ask this question because my entity-structure is a bit more complicated than a person with a name and a list of friends which may or may not be on Twitter (Video on Channel9: Helping you build dynamic JavaScript UIs with MVVM and ASP.NET). Here's my situation:
I have a class PersonnelClass with this basic structure:
[Serializable]
//The interface is for the implementation of 'Name' and 'Description'
public class PersonnelClass : IPersonnelClassOrPerson
{
public PersonnelClass() : this(Guid.NewGuid(), "", "") { }
public PersonnelClass(Guid id, String name, String description = null)
{
if (id == Guid.Empty) { throw new ArgumentNullException("id"); }
Id = id;
Name = name;
Description = description;
Properties = new PropertyCollection();
}
public Guid Id { get; private set; }
public String Name { get; set; }
public String Description { get; set; }
public PropertyCollection Properties { get; private set; }
}
The PropertyCollection class and associated AbstractProperty class look like this:
[Serializable]
public class PropertyCollection: List<AbstractProperty> { }
[Serializable]
public abstract class AbstractProperty: IEntity, IProperty
{
public AbstractProperty(String name, String description = null) : this(Guid.NewGuid(), name, description) { }
public AbstractProperty(Guid id, String name, String description = null)
{
if (id == Guid.Empty) { throw new ArgumentNullException("id"); }
if (String.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); }
Id = id;
Name = name;
Description = description;
}
public Guid Id { get; private set; }
public String Name { get; private set; }
public String Description { get; private set; }
}
In my Controller, I create an instance of a PersonnelClassViewModel that has this structure:
public class PersonnelClassViewModel
{
public PersonnelClass PersonnelClass { get; set; }
public List<AbstractProperty> Properties { get; set; }
}
I fill this viewmodel with a new PersonnelClass and two test-properties to pass to my View like this:
var properties = new List<AbstractProperty>
{
new TextProperty("prop1", "descr1"),
new TextProperty("prop2", "descr2")
//TextProperty is derived from AbstractProperty
};
var vm = new PersonnelClassViewModel { Properties = properties };
return View(vm);
I get everything in my View as I wanted. From the View I want to create a new PersonnelClass with a set of selected properties. I have the fields for Name and Description and to add the properties I have a ListBox with the properties that already exist (for demo-purpose they came from the controller now). Through a bit of Knockout JavaScript code I can select items from this list and populate an HTML select-control () with the selected properties to add to the PersonnelClass. This all works fine, until I want to build up an object to pass back to the Controller and create the PersonnelClass.
My question is: what Knockout JS code is needed to build up this object and pass it to the Controller by submitting the form and in my Controller how should I receive this object, meaning: what type of object should this be (PersonnelClass, PersonnelClassViewModel, ...) ?
If any more info/code is needed, please do ask. Thanks in advance!
Update after answer of 'B Z':
I followed a few more of Steven Sanderson's tutorials about this to be sure I understand this, especially the one you provided in your answer. Now I have following code in my View to start with:
var initialData = #Html.Raw(new JavaScriptSerializer().Serialize(Model));
var viewModel = {
personnelClassViewModel : ko.mapping.fromJS(initialData),
properties : personnelClassViewModel.Properties,
selectedProperties : ko.observableArray([]),
addedProperties : ko.observableArray([])
};
ko.applyBindings(viewModel);
The variable 'initialData' contains the values I expect it to have but then I get the following error:
Microsoft JScript runtime error: 'personnelClassViewModel' is undefined
I have no clue anymore. Can anyone help me fix this?
Steven Sanderson has an example of how to to work with variable length lists and knockoutjs
http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/
Having said that, I think your problem isn't so much on the knockout side and more on the how to databind the data correctly on the server side. In the link above, Steven uses a FromJson attribute to model bind which you may find useful...
HTH