Edit one model on the view page of another - ASP.NET MVC - 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.

Related

Pass data to User Control ASP.NET MVC

I have a user control which shows list of latest announcements. This user control would be used in almost 90% of my pages. Now my concern is how to pass data to this user control for latest announcements.
My first approach is to make a base controller and in Initialise method I pass data to user control via ViewBag/ViewData. All my other controllers derive from this base controller. This looks nice but my concern is that it may become an overkill for some simple solution existing already out there. Also I would need to make sure that no controller ever fiddles with my Viewdata/Viewbag data meant for my usercontrol.
Please let me know if this is correct way of proceeding ahead or there exists some better solution.
Thanks
Assuming you have a "user control" (you should try to refer to them as partial view's in MVC) that looks like this:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Announcement>>" %>
This means your partial view expects a list of Announcement objects.
Now, the question is - where are you rendering this partial view?
You could be doing it from a master page, you could be doing it from a view, or you could be doing it from another partial view.
Either way, the code to render the partial needs to look like this:
<% Html.RenderPartial("LatestAnnouncements", announcements) %>
But - where do you get the announcements from.
Assuming you have a Repository/DAL/helper method to get the latest announcements - i think you should have the ViewModel's you require inheriting from a base ViewModel:
public class AnnouncementViewModelBase
{
protected IEnumerable<Announcement> GetAnnouncements()
{
// call DAL
}
}
Then any master/view/partial that needs to render the latest announcements partial should be bound to a ViewModel which inherits from that base view model.
In the cases where the master/view/partial is not strongly-typed (e.g dynamic view), you can stick it in the ViewData. But if you have organized your view's correctly this shouldn't be required.
Is this the kind of thing you're after? How to pass data from view to UserControl in ASP.NET MVC?
You should use RenderAction in this kind of scenario, so that you do not have bother to pass the required data in each action method of your controllers.
I think the best way would be to use #Html.Action. This would allow me to call my actions dedicated to my usercontrols data and I can call it from anywhere.

Model View Referencing Another Model View

I have an Article model and I also have a Comment model. My article show page is rendering a view from the Comment views. It is rendering a new comment form and a show comment form. If I reference a variable in the comment form, like #comment, does it look for the #comment in the article controller or does it look for it in the comment controller?
The views are independent from the controllers. Only one controller will be accessed based on your routing settings, regardless if you include code from another view.
However, it sounds like you're doing things the "wrong" way. Article-Comment is a classic example of a nested resource and is probably what you're looking for. Try this article.

Creating a simple forum in mvc.net

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.

ASP.NET MVC Catching 'Save' POST with RenderPartial

I have a asp.net mvc page which renders a record from a database, it uses RenderPartial to call another view which renders an editable list of items related to that record.
My problem is I want a since save / submit button which not only saves changes made for that record but also changes made in the RenderPartial part... I have created a method accepting POST in the RenderPartials controller but it doesn't get called? Any ideas? Or am I using RenderPartial wrongly? I did it this way so that I have a controller that handles the subset of data
Update:
I don't think I've been clear enough:
Imagine a situation where you have a page that is filled with information from lots of different tables in a database... for example imagine you have a record of a person, and then you have all the links they have to Organisations that you want to list on the page, so the page contains:
Individual Name, Email, etc...
AND
Organisation Link 1
Organisation Link 2, etc... from a link table
Because of the amount of data I want to render of the page, I figured using different controllers to render each part would make sense.. but then when saving the data do I have to use just one controller method or can I call one controller to another... I only have one form and one 'save' button for the whole page
I hope this is clearer?
You don't need to have a specific controller for each controller, although I suspect you meant to say "I have created a method accepting POST in the RenderPartial's action ..."?
When you accept the default html helper commands, it can sometimes get confusing which action will be called. For a particular web page, the tag will determine where to POST values. So the action called will be dependent on the controller/action you specify in your Html.BeginForm call.
For example, here's a form we use from an ascx page:
<% using (Html.BeginForm("InvoiceDetail" //Action
, "CompanyInvoice" //Controller
, FormMethod.Post
, new { #id = "InvoiceForm", #autocomplete = "off" }))
{%>
A form can post to any action, in any controller. Not that you'd want to, but it's quite flexible. You can also have multiple forms in a single web page now, which is great for complex forms.

What is the proper way to handle forms in ASP.NET MVC?

Forms + ASP.NET MVC = Confusing for me:
What are the preferred ways to setup your controller action for form posts?
Do I need to specify an attribute that has [AcceptVerbs(HttpVerbs.Post)]?
Should the controller action take a "FormCollection" or should I use ModelBinders?
If I should use ModelBinders, how?
How do I setup the form in the view?
Should I use the Html helpers like Html.BeginForm/Html.EndForm or simply specify the form tag myself?
How do you specify the controller and action to be used in a form (with either the Html helpers or with a manual form tag)?
Can somebody please show me both a simple view with a form ~and~ its corresponding controller action?
I'm trying to write a form with a single textbox that I can submit to the Home/Create action and pass the string from the textbox to it.
ScottGu's handling form edit and post scenarios is exactly what you're looking for. There's also form posting scenarios and even though it was written for preview 5, it's still mostly valid.
The template's AccountController.LogOn and .Register methods along with the corresponding views should give you the simple introduction you need.

Resources