Use MVC to create multiple ids and names with the same name but incremented number at the end of name - asp.net-mvc-4

I want to create multiple entities from a single form with parameters that have the same name. I'm trying to create an array starting with 1 and ending in the max number of items in the array. Does entity framework do this by default.
Example:
PersonName(1): "Bob"
PersonName(2): "John"
PersonName(3): "Mindy"
If I loop through collections using entity framework is there a preferred method for name and id attributes.
Html:
<input name="personname(1)" id="personname(1)" value="Bob" /><br />
<input name="personname(2)" id="personname(1)" value="John" /><br />
<input name="personname(3)" id="personname(1)" value="Mindy" /><br />
Also I noticed that when I use #Html.EditorFor it has some overloaded methods to name the id and name attributes. So would it be recommend to build these using the template name set to empty string, and the
htmlFieldName="personname" + "(" + i + ")";
Or is there a preferred technique?

If you use the Html helpers, it does this for you automatically, especially if you use the EditorTemplates. For example:
#for(int i=0, i<collection.Count; i++)
{
Html.TextBoxFor(m => m.collection[i].Name);
<input type="submit"/>
}
This creates the input elements with the proper indexing. The better way is to simply use editor templates though:
#Html.EditorFor(m => m.collection)

You can loop through the collection using for loop. Appending [index] to personname.
For example:
for(int i = 0; i < personName.Length; i++)
{
<input name="personName[" + i + "]" id="personName_" + i value=personName[i] />
}
Note, that ID value cannot contain brackets, braces or similar, that is why you need to use something like underscore plus index value.

The correct answer is you have to add the parent table as the Model. In every reference to any child fields or child tables's fields you have to refer to this parent model first. All child models must drill down in the EditorFor's HiddenFor's, TextBoxFor's etc... by calling on the parent model first. Without the parent it won't know how to correctly relate to it when its time to perform the save changes or when editing it needs to check the proper references. Both the parent ids and child ids must be listed out in order to combine all tables in a single form and then perform a save changes on the fields. Otherwise it won't include those fields or it will not be able to save that table correctly and not auto generate... yes I said "auto generate" the ids. If you try and auto generate the ids yourself it will make maintenance a nightmare.
There... I filled in all the information a noob will need to surpass the point farmers.
So why is this important? Because if the #Model does not point to the top most object that all other object are related to then it doesn't know how the reference relates. Example would be a Document table. But the Document table might have a foreign key to a Title table. That would be one to one. The Document table would still need to be listed first like so Document.Title.TitleName. In a one to many like in an Author... b/c there might be more than one Author.. you would still list Document first. As in Document.Authors ... author would be a collection so you would loop through each author. But at the very top even in Partials you would still only reference #modelDocument and not the author, that would be after the #Model portion in the content area. If you try and reference just the #model author portion. Now it doesn't understand how it relates to the document and you either have to add the document identifier inside the author table's foreign key for the documentid at some point later downstream after adding the author. This is unfortunate as its not necessary if you just started with the Document to begin with.
Of course this can get a little more complicated b/c you can have more than one document per author so a linking table to a documentauthors table would be preferable here... but still Document table would still be listed at the top of a partial view and even the main view in order to completely place the correct identifiers and names in an html page so that when the form is submitted it properly knows how everything is realted. Also the identifiers should be placed in the html helpers for hidden input fields if they are not to be displayed outwardly to the users.
Now to make it an incremented number of times and to start with a certain grouping... it would require a query string or a posting attributes to send the start and stop range for that group or list of authors, or list of books, or documents or whatever. Since the one to many and many to many relationships should be output in for loops, it can select the correct relationships to retrieve and select also the ones to be displayed using linq for the entity framework which is supplied in a private member variable that allow you to perform database crud ops.
For the point farmers... nothing.

Related

How to store system-created to-do list items?

In a to-do list where users have both system-created items and their own items, how would you store the items?
A to-do list can have a mix of items. The system-created items can be modified and deleted just like user-created items. There difference is the titles and description text for the system-created items are initially pulled from a configuration. Many users can have the same system-created items. E.g. if two users want to paint rooms in their houses, they'd both get "buy paint" items.
Option 1
Save the full system-created items (including the title & text) with the user-created items.
Pros: Flexibility for user modifications since the items belong to the user and are not dependent on a central item configuration.
Cons: Lots of redundancy because there will be many users all with the same items.
Option 2
Save references to a configuration for system-created items with the user-created items.
Pros: Flexibility for system modifications since if we want to say "buy X-brand paint" instead of "buy paint", the change is easily reflected for all users with this item.
Cons: System-created items have to persist forever in the configuration even if the item is no longer relevant for new to-do lists because otherwise the user's reference will be broken.
Other options?
Thank you!
My initial thought is - what is your requirement? Your user-flows and project road-map might contain information to inform your design.
From your question "system-created items can be modified and deleted just like user-created items":
This indicates that you are going to have to have a way to track modifications to your 'system-created' templates per user or convert them to 'user-created' messages when they are edited.
This is more complexity than you seem to need
It seems much simpler to create messages from system templates, then have them be regular messages.
A bit of extra storage is not going to break the bank
You have not mentioned any case where you would need to operate over only system-created to-do's. But in this case, you could include created-by metadata.
I think the key here is whether you need to be able to modify a "system todo", and that change to be reflected in all the "user todos"...
If that's a requirement (it sounds sensible to me), your only option is Option 2 - the real con of Option 1 is once you copied the "system todo" as a "user todo", you cannot tell anymore whether they're related...
I'd go for a model similar to this, with 2 entities/tables:
ToDoTemplate
Integer id
String name
String description
ToDoItem
Integer id
ToDoTemplate template
Boolean completed = false
?String name = null
?String description = null
When you create a ToDoItem, you create it based on a ToDoTemplate (it may be a blank template), and you set the name and description as null, reusing the template name/description... Only if the user modifies their own ToDoItem is when you store that value... i.e.
String getName() {
return this.name != null ? this.name : this.template.name;
}
This is the most flexible of the approaches, and the only valid in many situations... Note the con you mention:
Cons: System-created items have to persist forever in the configuration even if the item is no longer relevant for new to-do lists because otherwise the user's reference will be broken.
This is not a con really - as long as there's one ToDoItem that uses a given ToDoTemplate, the template is still relevant, and of course there's no reason to remove it...

Is there a way to do string replacement/substitution in sql?

I have some records in a CMS that include HTML fragments with custom tags for a widget tool. The maker of the CMS has apparently updated their CMS without providing proper data conversion. Their widgets use keys for layout based on screen width such as block_lg, block_md, block_sm. The problem kicks in with the fact they used to have a block_xs and they have now shifted them all -- dropping the block_xs and instead placing a block_xl on the other end.
We don't really use these things, but their widget configurations do. What this means for us is the values for each key are identical. The problem occurs when the updated CMS code is looking for the 'block_xl' in any widget definition tags, it can't find it and errors out.
What I'm thinking then is that the new code will appear to 'ignore' the block_xs due to how it reads the tags. (and similarly, the old code will ignore block_xl) Since the values for each are identical, I need to basically read any widget definition and add a block_xl value to it matching the value of [any one of] the other width parameters.
Since the best place order-wise would be 'before' the block_lg value, it's probably easiest to do it as follows:
Replace any thing matching posix style regex matching /block_lg(="\d+,\d+")/ with: block_xl="$1" block_lg="$1"
Or whatever the equivalent of that would be.
Example of an existing CMS block with multiple widget definitions:
<div>{{widget type="CleverSoft\CleverBlock\Block\Widget"
widget_title="The Album" classes="highlight-bottom modish greenfont font52 fontlight"
enable_fullwidth="0" block_ids="127" lazyload="0"
block_lg="127,12," block_md="127,12," block_sm="127,12," block_xs="127,12,"
template="widget/block.phtml" scroll="0" background_overlay_o="0"}}</div>
<!-- Image Block -->
<div>{{widget type="CleverSoft\CleverBlock\Block\Widget"
widget_title="What’s Your Favorite Cover Style?"
classes="zoo-widget-style2 modish grey font26 fontlight"
enable_fullwidth="0" block_ids="126" lazyload="0"
block_lg="126,12," block_md="126,12," block_sm="126,12," block_xs="126,12,"
template="widget/block.phtml" scroll="0" background_overlay_o="0"}}</div>
What I would prefer to end up with from the above (adding block_xl):
<div>{{widget type="CleverSoft\CleverBlock\Block\Widget"
widget_title="The Album" classes="highlight-bottom modish greenfont font52 fontlight"
enable_fullwidth="0" block_ids="127" lazyload="0"
block_xl="127,12," block_lg="127,12," block_md="127,12," block_sm="127,12," block_xs="127,12,"
template="widget/block.phtml" scroll="0" background_overlay_o="0"}}</div>
<!-- Image Block -->
<div>{{widget type="CleverSoft\CleverBlock\Block\Widget"
widget_title="What’s Your Favorite Cover Style?"
classes="zoo-widget-style2 modish grey font26 fontlight"
enable_fullwidth="0" block_ids="126" lazyload="0"
block_xl="126,12," block_lg="126,12," block_md="126,12," block_sm="126,12," block_xs="126,12,"
template="widget/block.phtml" scroll="0" background_overlay_o="0"}}</div>
I know how to do it in php and if necessary, I will just replace it on my local DB and write an sql script to update the modified records, but the html blocks can be kind of big in some cases. It would be preferable, if it is possible, to make the substitutions right in the SQL but I'm not sure how to do it or if it's even possible to do.
And yes, there can be more than one instance of a widget in any given cms page or block. (i.e. there may be a need for more than one such substitutions with different local 'values' assigned to the block_lg)
If anyone can help me do it in SQL, it would be greatly appreciated.
for reference, the tables effected are called cms_page and cms_block, the name of the row in both cases is content
SW

Volt: Equivalent of destroy_all?

I have a form with a "reset this collection" button. Looks kind of like this:
<button e-click="reset_patients">reset patients</button>
In my controller, I do this:
def reset_patients
puts "destroying"
store.patients.each{|p| p.destroy}
end
What I expect is that the clients displaying the list will show an empty list. What is actually happening is that some but not all of the items are deleted.
How is a "dump the entire collection in the trash can" operation handled on a persistent backed store (i.e.: model :store)? Also, is there a way to make these cascade through related collections?
We don't have .destroy_all yet. Its on my short list, but I'm reworking one thing in the data provider API to make it a bit smarter. For now you can do
store.patients.reverse.each(&:destroy)
(The .reverse is needed since your deleting array objects as you loop)

How to display custom fields in rally?

I have a ruby script that is trying to pull up some custom fields from Rally, the filter works just fine ( the filter contains one of the custom fields i want to pull up) but when I try to display it, it doesn't show up in the list (the value returned for all custom fields is blank, while appropriate values are returned for FormattedID, Name, Description).
Here's the link [link]http://pastebin.ubuntu.com/6124958/
Please see this post.
Do you fetch the fields?
What version of WS API are you using? If it is v2.0 is c_ prepended to the name of the field in the code?
How is the field spelled in your code and how that spelling compares to Name and Display Name of the field in UI?
There is another reason why custom fields might not appear (other than the versioning and 'c_' issues nickm mentioned). I just found this out after a ton of head banging. The Rally SDK's ui stuff will filter out all fields that are hidden (such as _ref, or other 'hidden' custom fields) so you cannot view them in your apps in grids, charts, etc. For example, when constructing a Rally.ui.grid.Grid, a class called Rally.ui.grid.ColumnBuilder is constructed and it executes a command on the columns of the chart that look like this:
_removeHiddenColumns: function (columns) {
return _.filter(columns, function (column) {
return !column.modelField || !column.modelField.hidden;
});
},
As you can see, if you try to display any fields that are hidden (like _ref for example) in a grid, the column gets removed. So, although you may be fetching the custom fields, they will not show up unless those fields are not 'hidden'.

Grails trouble saving object id

id is present in the params but does not save to the db. for example
[book.id:1, comments: good]
comments will save to db but book.id does not.
I need to figure out how to save params.book.id into the db column BOOK_ID that was created from the hasmany/belongsTo relationship.
By default value attribute of each element will be the result of a toString() call on each element. Setting it as optionKey allows the value to be a bean property of each element in the list.
Kindly change the select to
<g:form action="review">
<g:select name="book.id"
from="${item.Book.list()}"
value="${book?.id}"
/>
<g:textField name="bookreview" value="${params?.bookreview}" /><br>
kindly refer the below link
Grail example with optionkey
You have overwritten toString, therefore your books are always rendered by name and not id. If you remove your toString() method you will see the expected results.
If you want your select box work with the name, just add optionValue="name".
Well, it seems that I had misspelled a crucial parameter inside my domain. So the domain was expecting x and it was getting y. Thus the id was always null. It took me a while to spot it, and I was perplexed because I had previously successfully implemented ids in another project. Grails has some jira issues also that make one consider other possibilities, but this time it was my mistake.