How Do I Submit Information from Multiple Partials? - asp.net-mvc

I have a page with a form on it. I dynamically generate the form based on information i have available to me in the database, but it works out to look a little like this:
MainView
-Partial1
-Partial2
--Partial2a
--Partial2b
All of the partials contain parts of the form. I've placed breakpoints in the code, that show me on the controller (in this case "Create"), the object does not have any of the information from the form populated in it.
This is the code i'm using for the 'create' button:
<button asp-action="Create" asp-controller="LSAA">Create</button>
How do I make sure that my post includes the information from the MainView, as well as the information from all the partials rendered as part of that main view?

The main view and the partials all render as one view from a browser, and call resources (js, css.. etc) from the perspective of the main view. If anything goes wrong - it means the configuration of the partials into the main view is probably the issue. When you say you don't get information from the partials do you mean it's not posted to the database? Or do you mean form data from that partial specifically does not appear in the view?

Related

How to accomplish this MVC layout

Being relatively new to MVC I have been struggling for the past several weeks getting my layout to work.
I have managed to get myself really twisted into knots. So instead of trying to explain and unravel my mess perhaps instead someone could explain how I would accomplish the following at a high level.
_Layout this would have all the css js etc. It would also have basic structure.
Of course HTML tags not allowed in code block....each render is in a div.
#RenderPartial(Header)</div>
#RenderBody()</div>
#RenderPartial(Footer)</div>
RenderBody is Index.cshtml and it would be broken into three pieces
#
#Html.Partial(NavMenu, model)</div>
#Html.Partial(SubNavMenu, model)</div>
#Html.Partial(MainContent, model)</div>
I have this basic layout and it looks fine until you click one of the menu items.
The menu items render as:
<a class="k-link" href="/stuffroute">Stuff</a>
That route goes to a controller that returns a view and that navigates away from the above arrangement in Index.cshtml. So I end up with the header, footer, and subdash nav....
So the question is...
How do I route / orchestrate my layout to not lose the differing pieces?
Partials don't do anything for you here. You're essentially asking about how to create SPA (single page application), although in this case your application will have other pages, it's just that the index view will act like a SPA.
That requires JavaScript, specifically AJAX, to make requests to endpoints that will return HTML fragments you can use to replace portions of the DOM with. For example, clicking "Stuff 1" causes an AJAX request to be made to the URL that routes to FooController.GetSubNav([stuff identifier]). That action then would use what was passed to it to retrieve the correct sub-nav and return a partial view that renders that sub-nav. Your AJAX callback will then take this response, select a portion of the DOM (specifically the parent of the sub-nav) and insert the new HTML as its innerHTML.
If you're going to be doing a lot of this, you'll want to make use of some client-side MVC-style JavaScript library, like Angular for example. These make it trivial to wire everything up.

ROR: Nested views

I have a page object and various template objects in my application. pages have names, descriptions urls etc and each have a relationship with a template. templates have different numbers of content boxes and relationships with other controllers (like blogs, galleries etc.).
When I am rendering a page I can work out what template is attached to the page, and what the relevant content is based on that. but I am not sure what the best way is to render the nested Items.
Are you meant to somehow render the templates view from within the other view? Or would you have to just rewrite the view altogether? In this case would I have to create an extra template view for each different template, bundle it with the page views, and then only include it if it is the right one?
Would this be the same for galleries and blogs? do they all need to be bundled with the page? Or can it be called from its proper location?
I'm not sure what the best practice is here and haven't had any luck googling it. I'm suspecting that the key words im using aren't correct. Or this is common knowledge that isn't worth documenting.
You can use shared partials to render views. Check out this guide.
In the views, you can render the partials based upon whatever condition you want.
For example:
- if params[:page] == "my_page"
= render "shared/my_page"
Naturally, you will still need to set up the needed data in the controller.
Shared logic for this can be placed in the Application Controller.

Rails 3 render partial without reloading images

I have a view 'edit.html.erb' which renders a partial.
<div id="exerciseslist"><%= render 'exerciseslist' %></div>
Inside 'edit.js.erb', I have the following line which reloads the partial each time something is updated.
$('#exerciseslist').html('<%= escape_javascript(render("exerciseslist")) %>');
The partial is a table with a bunch of items including images for each one and some buttons. All that is changing in the partial is the button. If a submit is successful, the button toggles state indicating that it has been added.
It's working fine, the problem is I don't like the way the images flash as the request happens, the items are also inside a scrollable section which looses it's state each time.
Is there a good way to just load the state of the buttons without rendering the full partial? Or is there a simple way to stop the client requesting the images each time?
I have done some reading and found pjax and backbone.js, are either of them a good way or is there a simpler rails way to do this?
Thanks,
Mike
Without seeing code, it's hard to be sure, but I think it could be done with just Rails. Instead of re-rendering the exerciseslist partial on submission, you could write an AJAX request to handle submission of the new data and on success, have the callback change the button state, rather than reload the partial.

Rails 3: How to render an action from another controller's view

I'm trying to call an action from a different controller's view:
Controller Countries has an action called selectbox which generates a html selectbox from all countries as a partial.
Controller Customers has an action called new which generates a html form for all the customers attributes. I want to insert the selectbox from Countries#selectbox in this form.
What would be the right way to achieve this?
You're doing it wrong. If there's a piece of code that is to be reused (such as html selectbox generation), you should put it in a helper and/or use a partial to render the html selectbox part.
Bear in mind this is only good advice if the code is somewhat complex (say, more than one or two lines). Here's a post I found while googling that may help you: helper or partial
For what you are doing, extracting the code into helper method is the right way to do it. However, if you still want to use am action from another, this is the official Rails plugin you can use:
https://github.com/rails/render_component

Updating part of a web page on Rails

I have a Rails controller with a form, and I want that when I post this form, a table on this page is updated via AJAX. I know a way, using partials to achieve this, but is that any way to do this without partials? And without putting code for my view inside my controller too.
Thanks
You can make a .js.erb file as the view, and from the form call the action from link_to_remote. That will translate into an ajax call to the action, that will then execute the js from the view. Inside that js.erb file you can do whatever you like. Although it will be hard to render part of the table server side if the code isn't broken out into a partial.

Resources