last day (15 days later) » 

11:15 PM
I assume you have multiple books.
Do you have one or more libraries?
 
Yes. There are several libraries. The index lists them all with CRUD operations available.
 
I think following REST principles is very beneficial to structuring your controllers
 
So, I'm really taking about the Create & Edit controllers for the library.
 
Let me write up a quick demo of how I would structure it
 
Thanks buddy.
 
11:21 PM
What is this room for?
 
Some asp MVC issues.
 
Maybe I'm thinking about it wrong, but I'd like to save the entire library and its books back all at once, when the user is done editing.
 
[RoutePrefix("libraries")]
class LibraryController : Controller
{
	private ILibraryRepository _libraryRepository;

	[Route("{id:int}"]
	[HttpGet]
	public ViewResult Get(int id)
	{
		return View(_libraryRepository.Get(id));
	}

	[Route(""]
	[HttpPut]
	public ViewResult Edit([FromBody] Library library)
	{
		_libraryRepository.Update(library);
		return Redirect("index");
	}

	[Route(""]
	[HttpPost]
	public ViewResult Post([FromBody] Library library)
	{
		_libraryRepository.Insert(library);
		return Redirect("index");
It's been a while since I used MVC so some parts of Web Api might have crawled in, but I think with ASP.NET 5 they have become one anyway
You can simply update your library by passing that library object with all its data (this includes its books) to your controller
 
Right. On the Library/Edit/Id post.
The trouble is, how do I add a new one to the form? I wish I had the code in front of me...
The only way I was able to get the model to stay bound was to pass it to the Post handler.
What's the FromBody and FromUri attributes?
 
11:38 PM
I'm not sure what binding you're referring to
You load your existing one using LibraryController.Get, make changes to it in your UI and call LibraryController.Edit(object)
 
MVC only calls LibraryController.Edit(object) if it's coming from the form's post. So, how do I capture that sometimes I want to add an item, but sometimes I want to actually save?
 
The "save" and "edit" button are one and the same?
 
Maybe I just need to learn more about how this works internally. It all feels like magic still. I think that's my problem. It's not magic, I just don't understand it.
For the sake of argument, yes. The save button calls LibraryController.Edit(Object), which updates the db.
 
Well you could very easily have it in the same method
 
But I need a button that adds a book to the library inside the Edit view.
 
11:51 PM
when you add that book to the library, what do you want to happen?
 
I want to add an (empty) new book to the library, but not save it until the user clicks "Save"
I'm thinking about this wrong somehow.
 
An empty book?
Oh, you mean you want the "new book" dialog to show?
 
A new entry in a table of books.
No. I want to stay in the same view.
 
Correct me where I'm wrong:
The user looks at his overview of libraries.
He selects a library.
He presses "Add book".
A form shows with entries for the book's data.
The user enters the data.
The user presses the "Add book to library" button.
The user adds a few more books like this.
The user presses the "Save library changes" button.
 
The user looks at an overview of libraries
Selects Edit library
Library info and list of books is shown
Presses add book
*inline* a blank book record is added to the list
The rest is right more or less
The big issue I'm having, is I don't want to direct the user away to another view & controller mid edit.
 

  last day (15 days later) »