I am constructing a form wizard that guides the user through a form that has already been created and deployed. The model and controller should stay the same as the only thing of change is the view (guiding the user through each form field). What is the best (and easiest, if possible) way of accomplishing this task?
Even through the wizard, once the user saves their form, it gets saved to the same database via the same model and controller. From doing a bit research it seems that this is possible by obviously creating a new view, create a simple controller that extends the original controller, and routing the new controller to the new view.
Any suggestions are really appreciated. Thanks!
After doing some research, I figured out a couple of ways to accomplish my task:
A simple way is to create a wizard action within the form's controller. Have the wizard action render the wizard's view.
A better way is to create a wizard controller and have it subclass the form controller. Override the new action, and render the wizard view. This is the more preferred method because basic CRUD actions in Rails acquire REST for free. Thus, following this method will yield a RESTful wizard.
You should definitely look here:
http://railscasts.com/episodes/217-multistep-forms
Otherwise, to answer shortly, you can tell any action of your controller to render ay view you want.That's what is done in the basic scaffold controller:
render :edit
Related
in my rails app I need to generate a bunch of buttons/links linked to several models to enable users to run some maintenance tasks (coded in the Backend).
I found the concept of actions in rails admin here: https://github.com/sferik/rails_admin/wiki/Actions, but I'm not quite clear how to use it, any ideas?
I thought I could add the buttons to the edit action for a model, but no ideas how to.
Sooo, rails uses an MVC paradigm. It sounds like you have the View and Model components under control, but you are missing your Controller logic. The Controller, is a place that handles any View logic using Model methods... ideally.
So, if you already have an existing controller that you are trying to add "Controller Actions" to... you need to define your custom method actions you want to create in your config/routes.rb file.
This will look like the following:
Create a route for your new controller action.
Add new controller action definition to your Controller.
Call controller action in your view's button link_to tag helper
Handle your custom logic in your new controller action using model methods
This is the cyclical MVC nature of Rails. M (Model), V (View), C (Controller).
How do I associate a PartialView which will be used across the app with a Child Action? For example the PartialView could be a login bar at the top of the page which will need to have some associated logic (loading the model etc) which I would normally put in a ChildAction.
However, I don't know what Controller will be used. I think I could create a base Controller class with the ChildAction and then inherit from that but I was hoping there would be a more elegant solution.
The RenderAction method allows for selecting the controller:
http://msdn.microsoft.com/en-us/library/ee839451.aspx
I am writing a basic blogging application in ASP.NET MVC. I have a Post model which can be displayed via View.aspx. On that page I also want to have a form to submit a comment, but as View.aspx inherits the Post model, I don't know how to have it edit a Comment model.
In your controller you can use whatever model you like. So place a form in your view which points to Post/Comment/{postid} or Comment/Add/{postid}. After you added the model to the database redirect to the previous view.
tsv - create a few partialviews for the comments (strongly typed) and create a couple of methods on the comment controller:
GetComments(int postId)
AddComment(Comment newComment)
in your post view, call the partialview either via $ajax or directly in the page.
jim
The key thing to understand here is when your page 'inherits' a class (Post, in your case), this is purely so that the Model does not need to be cast in any way.
This does not restrict you from referring to other classes within the aspx page, nor does it prevent the page from having a form which posts to another Controller/Action that inherits from a completely different class (Comment, in your case).
Maybe if you posted some example code, we could suggest ways to make it work the way that you want.
As Jim said, you can create the necessary methods in a different or in the same Controller and call <% Html.RenderAction("methodName","ControllerName") %> in your view.aspx which will still inherit the Post Model.
Then, create usercontrols for the form to submit a comment and to display all the comments.
I am creating a very simple forum as my first MVC project. My database layout is rather simple:
[ForumThread]
Id
Title
[ForumPost]
Id
ThreadId
Message
ParentId // To tell which post this post should hang on to
Created
CreatedBy
Updated
UpdatedBy
I am creating a view for the ForumThread for displaying the list of threads and to be able to create a new thread.
There is a details view of the ForumThread which shows the thread with the underlying posts.
My question is how I the easiest way in the ForumThread details view, can display a view to create a ForumPost, without having to navigate to another page first?
You could create a partial view (.ascx) which will contain the form allowing you to create a forum post and include this partial in the details view:
<% Html.RenderPartial("~/Views/Home/PostForm.ascx"); %>
As mentioned by Darin, you simply need a partial view - within that view you can implement the form, bind to a different model etc and also handle any events etc by a different controller if needs be.
It's also nice to encapsulate areas of functionality into partial views - keeps the code looking clean and if you find yourself needing to use that form again, then it follows the DRY (Don't Repeat Yourself) principle - you just add another Html.RenderPartial() call into wherever you need it.
I'm trying to embed a small view snippet that steps through a model fragment that works fine when I embed it in a single controller and pass it to a view like so;
Controller:
return View(_entities.formTemplate.ToList());
View:
http://www.pastie.org/666366
The thing is that I want to be able to embed this particular select box in more than just this single action / view, from the googling I've been doing this appears that it should go into a shared view, but I'm not clear then on how I could populate the model within that view from the controller? (or maybe I'm completely missing the purpose for shared views?)
In the other MVC framework I'm accustomed to working with there is the concept of a filter where you can call code before or after any action and mod the model as it passes the controller and goes to the view, is such a thing possible in .net mvc?
Any assistance appreciated.
You'll want to use the HtmlHelper method DropDownList() in order to create a input:
<%= Html.DropDownList("id", new SelectList(formBuilder, "ID", "Name")) %>
You probably want to use a ViewUserControl here.
You have a couple of options if you go that route. If it's model data that is easily available, recreate it at the call site of your RenderPartial like so:
<%=Html.RenderPartial("ViewName", new ModelData())%>
If it's data that is dependent on the current model data, then you'll need to pass that data somehow to your partial view.
ASP.Net MVC also has the concept of before/after controller actions. You decorate your controller method with an Attribute that derives from ActionFilterAttribute. In there, you have access to OnActionExecuting and OnActionExecuted.