How to remove an item from a list of entities programmatically? - razor

DNN 9.10.1.0 / 2sxc 11.22.01
We're using 2sxc Content and C# Razor templates.
There are 2 content types, a "parent" and a "child" one. The parent has some general fields and a list of entities of type "child".
If we want to delete one of the child entities programmatically, this doesn't work because the child entity is still in this parent/child relation with the parent entity.
The workaround so far is to update the parent entity with all the child EntityId's except that one that should be deleted. Afterwards, the now not related child entity can be deleted.
Is there a nicer way to remove an entity item which is in a list of another entity?

At the moment this is the only way to do it with public APIs.
The internal APIs can always change, so you shouldn't use them.
If you believe this is a common need, do post an issue on github and we'll consider it.

Related

Reusable HTML Blocks in Sitecore

I'm looking for a way to create a reusable HTML widget for a website run on Sitecore 8. My original idea was to create a data template that basically consisted of a single Rich Textbox. The idea is that you could drop any number of instances of these widgets on a placeholder and it would render out the HTML from each instance of the widget on the page and in the correct placeholder.
Example:
A content item called /products/my-product is based off of "Product" data template
It consists of some fields on the Product template (maybe product name, price)
I'd like the ability for the content editor to quickly drop one or more instances of the HTML widget on the page (say, in the right rail or in a different placeholder on the sublayout. I know I could just throw a "notes" field on the product template, but I'd like to make it more dynamic so that they can add several instances of this HTML widget and place them anywhere they desire.
I quickly realized that because we need the ability for multiple instances of this widget, a data template was not enough because each instance of the widget would needs its own data to populate on the front. Thus, my idea was to allow the content editor to add HTML widgets as a child of the current item (so each item would have its own instance data). I don't think this will work because I don't know of a way to have these children tell the parent page which placeholder to put them in, so laying them out is a problem.
I also thought about somehow setting the placeholder name as a parameter or field on the data template for the HTML widget, but I couldn't figure out how to get Sitecore to dynamically add them to a placeholder when it glues everything together.
Is there a way to achieve what I'm trying to do? Seems like a reusable HTML (or other kind of widget, for that matter) would be a fairly common need. I feel like there's an easy Sitecore way to handle this that I'm missing and overcomplicating the solution.
From what I understand, you're looking for Datasource field of a component.
Basically you:
Create a data template which contains fields necessary for your component
Create a set of items using that templates
Allow authors to select one of them as the Datasource for your component.
It's built in Sitecore functionality.
Check blog post http://firebreaksice.com/using-the-datasource-field-with-sitecore-sublayouts/ or google for Sitecore datasource.
Other links:
http://www.nonlinearcreations.com/Digital/how-we-think/articles/2014/03/4-patterns-Sitecore-component-development.aspx
http://www.nonlinearcreations.com/Digital/how-we-think/articles/2015/04/Sitecore-templates.aspx
EDIT:
Read about Datasource Location field (defining the repository of datasources location) here: http://www.sitecore.net/learn/blogs/technical-blogs/getting-to-know-sitecore/posts/2011/01/handling-presentation-component-settings.aspx
Read about Datasources and MVC here: http://jockstothecore.com/sitecore-mvc-item-maze/

Add related data with ASP.NET MVC 5 Razor

Good day,
I am creating an ASP.Net site with MVC 5 and Razor views. I have a Parent class and a Child class, so that a Parent can have multiple Children. I am using EF Code first and have my 2 models with my DB context in the Models folder in my project. I have created scaffolded items for both.
What I have done is on the details of a Parent, I have a list of children, using a _Partial view. What I would like to do is from the Parent details, if you click on a 'New Child' Action Link, the Create for Child is displayed with the ParentId already selected and not editable.
What woukd be the best way to implement this? Would I need to pass the data along to the controller or in ViewBag? I do not know the correct term is for this type of layout/functionality (Master-Detail view?).
I have also been on the ASP.net site, but cannot find what I am looking for.
You can pass the Parent Id to the Create action using the Action Link. Perhaps something like this, where "item" is the name of the local Parent variable:
#Html.ActionLink("New Child", "Create", new { id = item.Id })
Your Create action can then pass it along to the form in the Create view.

CKEditor: Allow content in nested widget, which isn't allowed in parent

I'd like to know if it is possible to allow content in a nested widget which isn't allowed by the parent widget.
I tried to allow images in the nested widget (using "img[alt,!src];"). The parent doesn't allow images. I can place images in the inner widget but it's filtered afterwards (e.g. by switching to source code and back).
Unfortunately, as far as I remember this will not be possible. The whole content is first passed through the main filter, and then each of editables passes its content through its filter. Unfortunately, implementing widgets in CKEditor 4.3 (meaning - dozens of major relases since 3.0) we had many architecture limitations and this was one of them.
You can, however:
mark elements which should not be filtered with data-cke-filter="off" attributes, but this solution is pretty inconvenient, because all data-cke-* attributes are automatically removed when getting data from editor, so you would need to add it dynamically when loading data,
use editor.filter.addElementCallback() - with this you can disable filtering of a chosen subtrees. You will need to check context of the element that you're skipping, to allow these images only inside future widgets, but not outside them.

MVC 5 Cascading Data

I am working on an MVC 5 razor page (using Entity Framework 6) that will display Vendor information. Each vendor can have multiple classes, and each class can have multiple subclasses. What I want to do is display the vendor information at the top, show the list of classes below that info, and then have a third section with a list of subclasses for the selected class. My question is what is the best way to do this?
Currently I am trying to do it with the main page getting passed a model of type vendor, and then within that page, displaying two partial views via #Html.Action("ActionName", "ControllerName", value). I can pass the class list partial view the ID of the vendor when the page loads without issue, the real problem is figuring out how to pass the selected ID of a class to the partial view for subclass. I've tried using hidden fields, but I'm not sure how to pass the html.Action the value of the hidden field.
This is my first MVC application, so any help is much appreciated.
First off, you are saying that you are using partial views with #Html.Action() helper - this is not for a partial view, but rather to produce a link to navigate to another page...That being said, I think I know what you want to do.
Second off, you need to post more code to show you made an attempt - stack overflow is not a source of free development consulting work. I think I know what you are trying to do, but even posting some sort of UI mockup might make a general discussion easier. That being said, I won't give you any code in the answer, just a general idea of how I would approach the problem.
To start, you should have a dedicated controller for Vendor, VendorClasses, and whatever the third subclass is (I'm going to assume its called VendorSubClass for the sake of example) since you chose the MVC pattern.
Next comes the part about displaying all of the information on a single page. The only place I could potentially see it making sense to show all of that info is on the views for VendorSubClass (however only VendorSubClass properties should be editable here) and even then, it might just make sense to have really informative breadcrumbs instead.
You would need to pass an object to the view that contained the VendorSubClass, its parent VendorClass, and its grandparent Vendor information. If you're using entity framework, this is a snap (but you didn't say you were so I'm making my explanation more generic). Then, you can reference partial views by using the helpers:
#Html.Partial('VendorPartial', Model.Vendor)
#Html.Partial('VendorClassPartial', Model.VendorClass)
as you can see above, I am not passing IDs - but rather the actual properties(objects) themselves which the partial views will use to render the details. This overview should get you started and give you some stuff to google.

Why is directly manipulating the Rails form params hash considered code smell?

I have a Rails form with a parent model and nested attributes for potentially multiple children of another model.
The child model has an attribute which is manipulated in logic as an array, but is serialized to a YAML string using the Rails built-in serialize method.
Within the form, I display each individual member of the array so that the user can selectively delete members.
The problem happens when the user destroys all members. The form will not pass any value for the param to the Rails controller and when the UPDATE action is called, it ignores the attribute since there is no key for it in the forms params hash. This is of course a known problem with things like checkboxes, so Rails automatically puts 2 checkbox HTML elements for each checkbox, one hidden that only processes if the checkbox is checked off.
I'm not dealing with checkboxes here but rather hidden input text fields.
The solution I've implemented is to manipulate the params hash directly in the UPDATE action of the controller, like this:
params[:series][:time_slots_attributes].each { |k,v| v[:exdates] ||= [] }
Is this considered code smell?
Should I instead add an extra hidden field that is disabled and only gets enabled when the user removes the last member? This solution works as well, but it seems clunky to me.
This is dealt with in the NestedAttributes module by allowing a "_destroy" parameter to trigger a destroy call for that particular nested attribute:
http://apidock.com/rails/ActiveRecord/NestedAttributes/ClassMethods/accepts_nested_attributes_for.
If you're not using nested attributes (which you probably should be, it's pretty neat in a lot of situations) then yes, you'll have to handroll something yourself, by working out which values should have been present and doing something special with those.
This is far from an exhaustive answer...but after thinking about this problem, one issue I can see is that if future forms are built that leverage the same UPDATE action, unexpected behavior will occur, which violates the principle of least surprise. If at a later time a second form is built which does not expect to change values for the exdates attribute (since it does not pass them), the UPDATE action will write an empty array into the attribute anyway.
I've decided to solve this issue by adding a single hidden form field with a true boolean value and later to check for this value before setting all time slot exdates to an empty array. This way, if a future developer creates a new form that leverages the UPDATE action of the series controller, they won't get the unexpected behavior of their exdates being set to empty arrays. If they want to process exdates in their form, they need to have the same hidden form field with a true value. This seemed like a simpler solution then adding a class and table for exdates, migration, and the AR associations and adding another layer of nested attributes so that I'd have not only a parent and children attributes, put a parent, children and grandchildren. This solution is a bit like the Rails hack for dealing with checkboxes with a second hidden checkbox field in the form.