Creating a simple forum in mvc.net - asp.net-mvc

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.

Related

Proper way of creating object using a form in its parents view?

Let's say I have two classes:
News (hasMany Comment)
Comment (belongsTo News)
In single news object view (news/show.gsp) I'm listing comments belonging to it after it's content.
Then I want to have a form which will allow some user (but security isn't a case here) to add new comment, and then show the same (news) view again.
Seems easy, but I couldn't find the anwser:
What is the proper way of doing it in Grails?
If you run grails generate-all News and grails generate-all Comment on the command-line, Grails will generate views and controller actions that allow you to CRUD each entity. If you read the generated code for the create and save actions and views of CommentController, that should set you straight.

.Net MVC 4 View Engine WebForm

This is a quite interesting question, in my opinion.
I have a strongly typed view using the WebForm view Engine, I don't know if changing to razor would solve my problem.
PROBLEM:
I have one view with a list of cars, so of type IList <Car>.
And I have a button "Create a new Car" that popups, the popup is a form that is hidded and you call a jQuery UI command $('formName').dialog() to popup it, this form has the attributes of the possible new car, so probably a new view with a strongly typed Car. After fill in the form the database should be populated with the new car, and the list of cars should be refreshed using Ajax.
The main problem is that I can't use HTML Helpers to IList <Car> and for Car at the same time.
Briefly: What is the strongly type for that view ? Is it possible to define two views and the other one call using pop-up? Changing it to Razor would solve my problem?
Best regards,
Tito Morais
Don't mix the views for listing the cars and creating a new car together.
For instance, you can make a popup that dynamically loads a "_CreateCar" partial view, using jQuery dialog or similar component. Then when the partial view is completed, reload the list view using another Ajax call.
Maybe not so much an elegant solution is to create a complex view model like:
class ListAndCreate
{
public IList<Car> AllCars {get;set;}
public Car NewCar {get;set;}
}
IMO this is correct since that one view is responsible for listing all cars and creating a new one. Now, I'm assuming that your NewCar has values coming from your controller or something, where you need to pass a model to your view.
The other approach, that #Jonas mentions is also correct and more unitized. You could create a partial view _CreateCar with type Car, render it with Jquery/Ajax to load it into a dialog/popup and have the form POST to a Create(Car c) method in your controller.

in asp.net mvc what's a clean way to create an entity from the 'create' view of another entity?

I am trying to come up with a clean design for this -
I am using MVC to process orders, so I have an 'order' entity, with its own controller and views.
From the Create Order view I would like the user to add a 'Customer' entity. I have a controller and CRUD operations for 'customer'.
When someone creates a new Order I would like them to either
1) enter a customer name to see if that customer already exists, and if so, add that Customer to the Order, or
2) Create a new Customer then add that new Customer to the Order.
My problem is I am not sure of a good way to access the Customers from within the Order.
-do I create a partial view for Create Customer, then use that view in the Customer Create AND Order Create?
-then would I create a partial view 'SearchCustomers' that passes params to an action on Customer controller and that returns results? Would I be able to reuse this across the site?
You can see I am not sure about a few things - are partial views the way to reuse things? can partial views be reused across controllers and access different controllers from the ones theyre in?
I have gone through an MVC book and online tutorials but they all seem to use beginner examples, where Model objects don't contain other Model objects.
Thanks for help
Views never "access controllers".
The flow is:
Controller Action Method -> Creates View Model -> Hands it off to a View -> View displays it -> View can pass sub-models on to other partial views
Decomposing everything into smaller models and partial views (or using Editor templates) is a good approach. You can then assemble those smaller models into a larger view model for a complete page, or you can use the ViewBag to assemble them in a non-strongly typed manner.

with mvc.net is it possible to share a view between multiple actions

This is a noob question, but I will ask it anyway...
I'm wanting to create a page that will do basic CRUD operations on a list of items:
-display the list
-edit an item
-create an item
-delete an item
It is looking like I will need an action for each of this operations. This is good and understandable. My question is regarding the views for interacting with the user.
I want to have in-place editing, so the user clicks on edit and they can edit the details of the item in the list. In my current understanding, i will have to duplicate a great deal of the view between 'display the list' and 'edit an item'. however, this seems to be unnecessary redundancy and will make future updates more time-consuming as I will have to update each view.
Is there an easier way? Am I on the right/wrong track? Any other comments?
Yes, absolutely. You'll want to use the overload of View() that takes a string. The string is the name of the view to render:
public ActionResult MyAction()
{
return View("MyViewName");
}
The View() method can take the name of a view as a parameter, so you can render the same view from several actions. By default (if you don't specify a view name) the framework uses a view named as the current action. See here for details.
I believe what you should be looking into is rendering partial views, which are .ascx pages similar to UserControls in WebForms. They're basically shared partial views that you can use for the same purposes across many views.
If you look in the default project template you can find examples for items such as the Login control.
Edit: And as others have noted, you can also share views between actions. Had my own noob moment there too. :)

Edit one model on the view page of another - ASP.NET MVC

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.

Resources