0

I am new to ASP.NET MVC and I have a question regarding viewing entity relationships.

Say I have an entity called 'Person'. This holds the usual data relating to a person (Name, Email, etc). I also have a 'Notes' entity. Under EF, a 'Person' can have many 'Notes'.

I have a Person controller where I can view and preform CRUD operations on a Person object. I can show the notes in the view easily but what is the best way to allow a user to add/edit/delete these notes from the Person view? I am hoping to do this using AJAX and not have the user move to a completely different page to add/edit/delete a note.

Thanks in advance, ViperMAN.

tereško
  • 58,060
  • 25
  • 98
  • 150
Brian Daly
  • 587
  • 4
  • 15

1 Answers1

2

When they edit a note, popup a jQuery dialog pointing to your URL to edit or have a separate Ajax.BeginForm() on the page that the details go into. When they finish the edit call a method to refresh the notes.

So: 1. In your Notes grid (or whatever) you have an edit link for each note called "edit" this link looks something like the following:

This one actually uses 'notes' : )

http://www.iwantmymvc.com/dialog-form-with-jqueryui-and-mvc-3

ASP.NET MVC | Problem about showing modal dialog using jQuery dialog widget

ASP.NET MVC modal dialog/popup best practice

Also beware of this scenario for multiple links: MVC3 - Only first row link works well with Jquery Modal Dialog

Now the urls you use to populate the dialogs would be for example /Note/Edit/10 One thing to note - jQuery validation needs to know about these new items that are being loaded via ajax into the DOM , so in your partial view you need to tell jQuery validation to include the new items - I'll edit in a bit to add this, have to grab it from another machine.

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • @Adam- thanks for your answer. There is certainly enough there to get me started. What about the scenario where there is an Add button above the list of notes and clicking it would show the create note form on the page using AJAX without a popup? Is this easy to implement? Thanks in advance. – Brian Daly Jun 16 '11 at 20:01
  • instead of using a jQuery dialog, I would simply use the exact same partial view to edit a note and instead of loading it into a dialog, just use the jQuery .ajax call to load the results into a div, and then show the div. OR keep it super simple and use Ajax.ActionLink() and specify the UpdateTargetId="yourDivId" – Adam Tuliper Jun 16 '11 at 20:47
  • Hi Again, the solution above works perfectly but I am stuck on something. I have created a controller to work on the CRUD for a Person and for Notes. But each note needs to be linked to a Person. When creating a new note, how do I obtain a reference to the PersonID from the Notes controller? Thanks in advance. – Brian Daly Jun 17 '11 at 14:29
  • Since you are currently viewing a 'person' you will have to change your link to your action method for the create action. Either set this value as a hidden field in your DIV or if you call some 'get' action to display the data in this div, pass it over as a parameter. You will have to form this URL (ie actionlink) when the page initially loads with the correct person id. From a security standpoint you may want to validate upon save that this personID is really - the current person as hidden fields can be forged easily. – Adam Tuliper Jun 17 '11 at 14:34
  • Thanks for your timely respone Adam but I am having trouble understanding your answer. To start I want to get the Notes section working withlout the use of AJAX.I have a controller for Person (with CRUD ops) & a controller for Notes (with CRUD ops). When looking at the Details for an employee I have a link to the notes controller. This link passes the PersonID to the Index action of the Notes controller- it takes this personID and lists all the notes associated with that person. On the Index page the user can click create. Have you an example where hidden fields/parameters are used? – Brian Daly Jun 17 '11 at 15:17
  • Your notes controller - is is a strongly typed view to a ViewModel or just your Model from your database? If its a ViewModel (the recommended approach) then this is a model that uses only the fields that you want for this Index action. If its a model from your db directly then you must have a PersonId in there somewhere. On your "list" view you can do Html.HiddenFor(o=>o.First().PersonId) - the problem with that is you may not have any notes yet, so there is no 'first' record to read from. So the solution here is to either add a PersonId to your ViewModel, or if not using a ViewModel, – Adam Tuliper Jun 17 '11 at 15:56
  • you can set the PersonId in your controller code by ViewData["PersonId"] = whateverId and in your view you would have @Html.Hidden("PersonId"). Make sure the action for your 'create a new note' method has a parameter int personId. Hope this made sense.. if not let me know where its confusing and I'll refine a bit more – Adam Tuliper Jun 17 '11 at 15:56