I have 3 different partials, each representing a different step of this process: "_overview.slim", "_setup.slim", and "_submit.slim". I want to show these 3 different partials all in "show.slim" only one at a time, and one after another as the user clicks on "Go to next step". How can I accomplish this in the show action of my controller?
Let say in your show,have a 'Next' button to trigger the next partial.
In the 'Next' button, I can pass a param[:page1] and all the necessary param when click.
In the 'Show', if there is param[:page1] ,then display _partial1.html.erb
its goes all the same.
So if I were you, I would put render all three partials in your show view, and use Javascript, and CSS to manage when each one was shown. If you drop each of those partials in a div, its easy to use JQuery's hide and show methods, linked to the click event of the "Go To Next Step" button. To do it this way you wouldn't have to touch your Controller at all.
Related
I am new to ASP.Net MVC, and still trying to wrap my head around the controller and passing data to the view and back.
Here is my question. I have a model in my view with a property that is "isEnabled", which can be true or false.
I have an edit button, and an enable/disable button. Edit just takes me to a new view with a form.
I want the enable/disable button to change the property of the model to enabled or disabled.
Right now I have two separate buttons. When I click on them, it fires the appropriate action from the controller (EnableModel, DisableModel), and just reloads the current view.
How can I make it so, if the model is disabled the button shows and fires the enable action, and when it is enabled, the button shows and fires the disable action.
So here are the options I thought of.
1. Two buttons, I hide them as needed. I can use an if statement to check if the model is showing or not in razor.
2. Use javascript two to the above
3. Use javascript to physically change the button
What would be the best method?
Alright so looking back can't believe I ever even asked this haha.
I went the javascript route. I had a single button, and a simple onClick javascript class that would handle the toggling.
I am making a reddit clone. Right now I have a voting system that works, but it refreshes the page each time a user votes. I want to make this async by using AJAX.
Making buttons respond to Jquery is really hard though because they change state depending on how a user has voted on a post. The solution I came up with is this: Have 2 upvote buttons in the _upvote partial, and have 2 downvote buttons in the _downvote partial. Each partial will have one voting button that is activated when the case is already true, and clicking on it again will destroy it (for example, if a post is already upvoted on, then reclicking the upvote button will destroy it). The other button(s) in the partial will be activated in the case that the button has not been used yet (in which case clicking the button will create it).
Example:
****The _upvote partial should have the following buttons in it:****
1)create_upvote
2)destroy_upvote
****The downvote partial should have the following buttons in it:****
1)create_downvote
2)destroy_downvote
Whichever buttons are activated will depend on the controller.
My question(s) are this:
1) What is the actual syntax to a render partial view from another controller? For example: posts is rendering _post, and within that I want _upvote or _downvote to be rendered. How to render _upvote or _downvote from the controller. Again, the reason I WANT to render these partials from a controller is because the controller will be changing their state.
Downvotes partial
app/views/posts/_downvote.html.erb
Upvotes partial
app/views/posts/_downvote.html.erb
Posts partial
app/views/posts/_post.html.erb
Downvotes controller
Upvotes controller
Line 9 in the controllers is where my syntax is off! What is the syntax to render _upvote and _downvote partials, within the _posts partial? Thanks mates!
As far as syntax goes, what you want is the following:
render partial: "posts/upvote"
Instead of sending html data (the buttons you mentioned) from controller to the partial, you should have a conditional statement in each partial that activates one button or another. From there create event handlers for each respective button. That takes care of the issue of Jquery handling internal state.
Just a small note for the special cast of partials inside views/layouts/_upvote.html.erb, you should use
render partial: "layouts/upvote"
(not application/upvote)
I have an mvc app where I pass a list to a view. In a most click, I want to be able to render the next item in the last but am having trouble figuring out how to do that efficiently. My approach originally was to use an index i but I realized that one the page is rendered, accessing my model last at i will always leaf to the same result since that item in the list is rendered on page load and can't just be accessed dynamically. Any insight to an approach for this problem?
The model can't be accessed as it's only used on the server side.
There are a few ways of solving the problem, you can use Knockout.js or similar client side view model components, once the user click on the button just render then next item from the knockout model.
Or use AJAX to retrieve the next value from the back end and then render it to the screen.
Or generate the whole screen and just hide all items from the user and then display them once the user clicks the button
I am a newbie, i am trying to create a view where every time i click on add more link next to a Textbox, another text box appears. How do i do that? I searched the same question on this site but i could not find a solution.
You'll have to create an array (string[] Smth)in your model. Then you have to make your "add more" link to submit your form to controller action that will add another element to this array and roundtrip back to view.
You may take a look at the following article which illustrates how you could handle dynamic rows.
I have a created a list view for one of my controller's index action.
I have added a new column "Select This" to this list view, by using the view's source.
The column will contain radio button for each entry in the list. This i have achieved by just placing a radio button control from tool box in the "Select This" column. This i have done in the design of the view and when i run it, i get radio buttons, one for each of the entry. The page also has a link button and i want to call a controller action on this link button click which will receive the index of radio button selected. So if i select 5th radio button, how can receive 5 in the controller action.
How can i do this?
Regards,
kapil
If you used plain HTML, your radio buttons will all have the same name, but different value. You will get one result in the POST, which will contain the selected value.
If you used ASP.net helpers, it's the same, but they will be generated for you: see here: http://msdn.microsoft.com/en-us/library/dd410596.aspx You will pass the same name (first parameter) and different values for each input (second parameter).