Grails one-to-many relationship data binding - grails

I am new to Grails and am trying to test a sample one-to-many form as outlined in the link below
http://www.2paths.com/2009/10/01/one-to-many-relationships-in-grails-forms/
The views seem to be working fine as I can add new text field for Book (child) from Parent (Author) and delete as necessary.
However, the databinding for child data (Books) doesn't seem to be working.
Books that belong to the Author are not added to the database when I add them in the view.
It was my understanding that the default controller should work in this case. Am I wrong on that?
Do I have to modify the save() method for the AuthorController?
I see that the above post was from 2010 and I am currently using Grails 2.4.2.
Could that be the issue?

Related

Proper way of creating object using a form in its parents view?

Let's say I have two classes:
News (hasMany Comment)
Comment (belongsTo News)
In single news object view (news/show.gsp) I'm listing comments belonging to it after it's content.
Then I want to have a form which will allow some user (but security isn't a case here) to add new comment, and then show the same (news) view again.
Seems easy, but I couldn't find the anwser:
What is the proper way of doing it in Grails?
If you run grails generate-all News and grails generate-all Comment on the command-line, Grails will generate views and controller actions that allow you to CRUD each entity. If you read the generated code for the create and save actions and views of CommentController, that should set you straight.

How to model many-to-many relationships?

I have a problem with a model in a asp.net mvc3 application.
First of all, i have a table with items, each item has e.g.: id,name and description
where i also have a user with: id, name
It looks somewhat like:
class item {int id, string name, string description}
class user {int id, string name}
Now i also have a table which maps these two things to each other, with foreign keys.
so each user can have * items and each item also can have * users.
Now i need to create controller and view for the item. this works fine, the problem is, that i just create a item, but i want to create a item and the mapping to a user in one step.
how to solve this problem in mvc? (e.g. i just can add one model in a view and this is the item in the create process)
Short answer: you need to add drop-down of users while creating an item. This will help to link each item to a user.
However, your particulate issue seems to be in building many-to-many relationship in the model. How to do this? Well, this is already nicely explained here - ASP.NET MVC, Entity Framework, One-to-Many and Many-to-Many INSERTS
In addition, I would strongly suggest to go through - NerdDinner Tutorial. It will give you good start-up and solid background for development in asp.net mvc framework.
You may also refer to Tutorial samples in official website to learn and practice development in mvc.
Here is a link to complete code of the tutorial - NerdDinner 2.0 Complete ASP.NET MVC Sample App

ASP.NET MVC ViewModel to Hold Lists & "Images or YouTube URL's"

I'm posting this question because I do not know the best/correct way of doing the following.
My team-mate (the designer) sent me a good looking design that includes a wizard for adding new items (for auction). The user has to fill in all the required details which include the title, description, starting price...etc AND a list of tags (up to 4 tags - chosen from the database, will use auto complete) as well as a list of up to 3 images/youtube url's (for the sake of better explanation check this image out: http://i55.tinypic.com/2v11zzr.png)
Ok so I figured out how I should do the wizard ( reference: how to make a wizard with ASP.Net MVC) but I'm not sure about how to collect the lists and the images/url's. Here's what I'm thinking:
For the images/url's, I should create a parent view model from which two sub-classes (ImageViewModel & YoutubeUrlViewModel) would inherit from and then in the controller action when I parse the post data, I would check to see the instance of the parent view model and act accordingly.
Now about the lists, I'm not sure whether I should include a List in my view model or whether I should include 4 string properties representing the tags (the same will apply to the list of images/url's).
So what's the best way of doing this?
And Haacked to the rescue: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
:)

Where to put Model-specific code when using .dbml designer?

I'm building an asp.net mvc application for users to enter an essay contest. I have an Essay table in sql server 2005. I created my domain models by dragging over the tables in the server explorer and saving the layout as, what I've named DAL.dbml.
I'm now trying to implement input field validation in the business layer using methods mentioned in Chapter 11 of Pro ASP.NET MVC Framework by Steven Sanderson. But, the author didn't create his models like I am, so I'm at a loss as to where to put my model-specific code.
I considered putting it into the auto-generated DAL.designer.cs file, but that seems problematic.
I also tried creating a new class, EssayRequired.cs, where I made EssayRequired extend Essay. This almost worked.
It successfully showed all of the validation errors.
But when it comes time to actually save the record:
EssayTable.Context.Refresh(RefreshMode.KeepCurrentValues, essay);
EssayTable.Context.SubmitChanges();
I'm getting this error:
The type 'DomainModel.Entities.EssayRequired' is not mapped as a Table.
Any ideas as to what I should do now? Is there a better place to put domain-specific code that won't get wiped out if the db table changes and I have to delete and re-drag the table over?
Or is there a way to tell the app that the EssayRequired object should land in the Essay table.?
Thanks, Scott
You can create a partial Essay class in the same namespace as the dbml file.
public partial class Essay
{
partial void OnCreated()
{
// handle additional stuff on creation, for instance.
}
}

Problems with model using LINQ to SQL when adding a strongly typed view

I am trying to create a simple task manager solution based on the Nerd Dinner tutorial
weblogs.asp.net/scottgu/archive/2009/04/28/free-asp-net-mvc-nerddinner-tutorial-now-in-html.aspx.
EDIT: I have removed the http:// on these urls because I have not got enough rep to add links into a post.
I have built my model as shown here: nerddinnerbook.s3.amazonaws.com/Part3.htm
It is identical except that Dinner is a task and RSVP is a project.
Relationship task.projectId -> project.projectId
I have more fields in these tables but I have kept the public partial task class in the model simple so far to match the tutorial.
My question is that when I try to add a new view and in the dialog I select "strongly typed view" my model class for the task does not show up in the drop down, anyone know why??
Probably a bit vague, i am just trying to get some ideas on why this could be happening.
I thought maybe my namespace was incorrect somewhere or my class was not public but it is.
I have got a reference to my repository in my controller by doing
TaskRepository taskRepository = new TaskRepository();
and the controller has a using reference to TaskManager.Models;
All confusing me.
I don't know how the dialog picks classes (other than it requires a clean compile), but you can just choose any random class and then edit the first line of the resulting aspx to substitute the class you prefer.

Resources