I am trying to build a very simple widget with two input fields Name and Email address. Right now I am following http://www.deepcode.co.uk/search?q=how+to+build+a+widget+without+a+model+in+Orchard.
Migration.cs
public int UpdateFrom22()
{
ContentDefinitionManager.AlterTypeDefinition("QuickLinkWidget", builder => builder
.WithPart("QuickLinkWidgetPart")
.WithPart("CommonPart")
.WithPart("WidgetPart")
.WithSetting("Stereotype", "Widget"));
return 23;
}
ContentPartRecord (Models/QuickLinkWidgetRecord.cs)
public class QuickLinkWidgetRecord : ContentPartRecord
{
//We shall not define any property for this class.
//We shall not create any HANDLER for this widget because we do not need to store any data to our database.
}
ContentPart: (Models/QuickLinkWidgetPart.cs)
public class QuickLinkWidgetPart : ContentPart<QuickLinkWidgetRecord>
{
//Again we shall not definte any Record property here......
//We shall not create any HANDLER for this widget because we do not need to store any data to our database.
}
Handler
Did not define any as I don't have deal with any repository.
Driver: ((Models/QuickLinkWidgetDriver.cs))
public class QuickLinkWidgetDriver : ContentPartDriver<QuickLinkWidgetPart>
{
override DriverResult Display(QuickLinkWidgetPart part, string displayType, dynamic shapeHelper)
{
//We do not have any property defined in Model class.
//So we don't have to assign any Default values to anything!
//We shall just return a SHAPE of the widget
}
}
Placement.info:
<Place Widgets_QuickLinkWidget="AsideFirst:1" />
And finally the View: Views/Widgets/QuickLinkWidget.cshtml
#using Emfluence.Intrust.Models;
#model Contact
<h2>Our Side link widget</h2>
Nothing extra I added in my View
Please help me understanding what I am doing wrong. Why I am not able to see the widget rendered at all? I am stuck with it for the last 3 days!, just want to see it rendering first!
Thanks in advance fro your help
I had similar issue and i got it working by changing some setting on my placement.info file...in your case try this
<Place Widgets_QuickLinkWidget="Content" />
Related
I have a content MyContainer which has a ​Container part attached to it.
Then i have several MyContainerItem contents which have a Containable part attached to it and these are assigned to MyContainer.
Now, if the content MyContainer is rendered, it internally uses the List shape and therefore each MyContainerItem content is rendered with the Summary display type, which seems hard-coded in Orchard.
Is there any way to change the display type of MyContainerItem to Detail without altering the core code?
I tried the shape shifting technique in my alternates but to no avail.
I've had this issue before on other content parts (don't remember which), but what you could do is take the following steps:
1 Create a custom part and record to attach to the content types that have the ContainerPart:
public CustomContainerPart : ContentPart<CustomContainerPartRecord> {
public string DisplayType {
// property setter and getter
}
}
2 Create a handler that attaches the CustomContainerPart on the content types that have the ContainerPart:
public class CustomContainerPartHandler : ContentHandler {
private readonly IContentDefinitionManager _contentDefinitionManager;
public CustomContainerPartHandler(IContentDefinitionManager contentDefinitionManager) {
_contentDefinitionManager = contentDefinitionManager;
}
protected override void Activating(ActivatingContentContext context) {
base.Activating(context);
// weld the CustomContainerPart dynamically, if the type has the ContainerPart
var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(context.ContentType);
if (contentTypeDefinition == null) {
return;
}
if (contentTypeDefinition.Parts.Any(part => part.PartDefinition.Name == typeof(ContainerPart).Name)) {
context.Builder.Weld<CustomContainerPart>();
}
}
}
3 Create the driver for the custom part and use this Display method:
protected override DriverResult Display(CustomContainerPart part, string displayType, dynamic shapeHelper) {
return ContentShape("Parts_Container_CustomContained", () => {
var containerPart = part.As<ContainerPart>();
// copy code from Orchard.Core/Containers/Drivers/ContainerPartDriver.cs here and tweak it to use the CustomContainerPart display type property
// ..
var listShape = shapeHelper.List();
listShape.AddRange(pageOfItems.Select(item => _contentManager.BuildDisplay(item, part.DisplayType))); // here use your custom part setting
// ..
});
}
4 And finally your Placement.info:
<Place Parts_Container_Contained="-" />
<Match DisplayType="Detail">
<Place Parts_Container_CustomContained="Content:7.5"/>
</Match>
Hmm, i was approaching it the wrong way.
It came to my mind that alternates would be the better solution as it doesn't alter the list behavior.
Now i understand it this way: list should normally show a summary of each item and when i click on the item the details are shown, makes sense.
So instead of changing the DisplayType just create an alternate with the appropriate name according to the docs:
http://docs.orchardproject.net/Documentation/Accessing-and-rendering-shapes#NamingShapesandTemplates
In my case the alternate would be Content.Summary-MyContainerItem.cshtml with the following content:
#Model.Content.Items[0].Html
I need to send 2 different Models, one to Index view and another one to _Layout.cshtml, how I can do it?
My HomeController:
[Route("")]
public ActionResult Index()
{
HomeViewModel model = new HomeViewModel();
model.A = _repoA.GetLatest(4);
model.B = _repoB.GetLatest(4);
model.C = _repoC.GetLatest(4);
return View(model);
}
I don't like using ViewBag, ViewData & ..., I'm looking for passing the model in same way as we passing model to Views.
You can place this in your Layout to load a partial each time... Pretty useful for loading in a piece of a dynamic menu or a widget on each page.
Along with this line in your layout you can just do your Index page as you normally would.
#{ Html.RenderAction("_widget", "Home"); }
You'll need to send it along in the ViewBag. I found the best bet was to make an abstract controller:
public abstract class ApplicationController : Controller
{
protected ApplicationController()
{
UserStateViewModel = new UserStateViewModel();
//Modify the UserStateViewModel here.
ViewBag["UserStateViewModel"] = UserStateViewModel;
}
public UserStateViewModel UserStateViewModel { get; set; }
}
Then, have all of your controllers inherit from this abstract controller.
In your _Layout.cshtml (or whatever you called it), you'll need to include the following at the top:
#{
var userState = (UserStateViewModel)ViewBag.UserStateViewModel;
}
Duplicate but refined from the 2nd answer to ASP.NET MVC Razor pass model to layout.
I have an "Embedded Resource" view. In this view I am using the following model
public class TestModel
{
public TestModel()
{
CustomModel1 = new CustomModel1 ();
CustomModel2 = new CustomModel2();
}
public CustomModel1 CustomModel1 { get; set; }
public CustomModel2 CustomModel2{ get; set; }
}
In this view I have a form and inside it I am using #Html.EditorFor instead of #Html.Partial, because when I use #Html.Partial the CustomModel1 passed to the action (when the form is submitted) is empty.
#Html.EditorFor(m => m.CustomModel1, Constants.CustomEmbeddedView1)
However when I use #Html.EditorFor and pass as a template a "Content" view
#Html.EditorFor(m => m.CustomModel1, "~/Views/Common/_CustomPartialView.cshtml")
I get the following error:
The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.Int32'.
If I set the "Content" view to be an "Embedded Resource" everything works fine.
Is there any way to resolve this problem? Perhaps there is another solution to the model binding problem instead of using #Html.EditorFor.
I found a solution to my problem. I still do not know why the error is thrown, but at least I fixed the model binding.
The problem with the model binding, is that when you call #Html.Partial
#Html.Partial("~/Views/Common/_CustomPartialView.cshtml", Model.CustomModel1)
The elements that are dispayed (I use #Html.EditorFor(m => m.Name) for example in the partial view) have an id="Name". So the model binding tries to find a "Name" property inside the TestModel, but the name property is inside the CustomModel1 property. This is why the model binding does not work, and the Name property is an empty string when the form is submitted.
The fix is to set the HtmlFieldPrefix.
var dataDictCustomModel1 = new ViewDataDictionary { TemplateInfo = { HtmlFieldPrefix = "CustomModel1" } };
#Html.Partial("~/Views/Common/_CustomPartialView.cshtml", Model.CustomModel1, dataDictCustomModel1 )
This way the id of the Name property becomes id="CustomModel1_Name", thus allowing the model binder to properly set the value of the Name property.
There may be a better solution for this, but so far this is the best, that I have come up with.
I have an app (written using MonoTouch and currently working) that I want to add landscape orientation to. I am using a UITabBarController.
I don't see how to create a controller that will allow me to override the "ShouldAutorotate..." method. Can anybody point me to an example using a UITabBarController in MonoTouch?
TweetStation contains a sample precisely for this setup, and propagates the rotation down all of the nested view controllers.
Are you subclassing UITabBarController?
You are probably non subclassing and just adding a vanilla controller in Interface Builder. You have to subclass to override that property.
First make a new class like this:
//Test this, it's off the top of my head
[Register("YourTabController")]
public class YourTabController : UITabBarController
{
public YourTabController (IntPtr handle) : base (handle) { }
[Export("initWithCoder:")]
public YourTabController (NSCoder coder) : base (coder) { }
//Override should rotate
public bool ShouldAutoRotateToInterfaceOrientation(UIInterfaceOrientation o)
{ return true; }
}
Then, if you already have a UITabBarController in IB, there is a 'Class' property that you set to the name of your new class.
This seems like it should be simple, however, I cannot find an example through searching...
How do I update a label on a usercontrol using variables from its parent form?
I am building a compact framework 3.5 application.
Thanks for any and all assistance!!!
You can create a public property on the user control, and you update your label from there, for example:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public string LabelText
{
get
{
return label1.Text;
}
set
{
label1.Text = value;
}
}
}
You can also return the label itself, although some people may say that would break encapsulation on your design.