Reputation: 2013
I'm trying to grasp my head around the best way to carry out something.
I'm creating a very simple forum, using ASP.NET MVC and LINQ to SQL. Very simple means it has no categories, and each message consists of a subject, a composer and the message contents. Comments can be threaded, but only go one level deep (if you comment on a child message, it will appear as though you commented on the parent message of that child).
Oh, I also properly paginated the forum.
Now, the ASP.NET MVC flow bit I'm trying to understand:
When someone goes to Home/Forum/{page}
I create a model for the view with 2 objects - a list of all the messages to show (in the proper order of parents and children. I create the list server-side), and a forumMessage newMessage
object, so if the user creates a new message, or replies to an existing message, I can easily populate the object and send it back to the server.
However, after reading lots of articles on validation, models, etc., it seems to me as though I would be better off using broken-down message fields to be filled out by the user (i.e: String messageComposer; String messageComposer; String messageContents;
), since it will be easier to define validation rules for them.
This is for two reasons:
1) since the forumMessage
object is created using LINQ to SQL, it's kinda of an issue to define its validation rules (yes, I know you can use buddy classes, but I'm kinda scared O_o )
and what seems to be the bigger reason 2) in all validation examples, if the data that was needed to be validated was passed as an object (i.e forumMessage
), then it was the only object in the model. I saw no reference to scenarios in which the model consisted of data which was also used simply to populate the views (in my case - the forum messages for the particular page).
So far, whenever I had to get input from the user, I passed along the objects which I wanted to display or populate as the actual LINQ to SQL objects.
Was I going about this the wrong way? Should I pass LINQ to SQL objects (i.e. classes) for display purposes, but use plain old fields (strings, ints, etc.) for the data I want to retrieve, and create the actual LING to SQL object server side, when I get all the data back to the controller?
I'm at a crossroads here, and would appreciate some help from seasoned veterans :-)
Thank you!
Upvotes: 0
Views: 943
Reputation: 8645
I'm creating a very simple forum
As it's a simple forum, then the simplest approach would be best, i.e. using Linq-2-Sql object through the entire process (i.e from DAL to View)
Regarding using a mix of objects/properties, I would use a separate ViewModel to aggregate the data.
e.g.
// class used for the strongly typed view
public class ForumMessageViewModel
{
// linq-2-sql class
public List<Message> Messages { get; set; }
// contains data that you want to get back
public NewMessage NewMessage { get; set; }
}
// represents a new message
public class NewMessage
{
[StringLength(100)]
string MessageComposer { get; set ;}
}
Your View (cut down, simplistic version)
@model ForumMessageViewModel
<!-- render the messages -->
...
<!-- entry for new message -->
@using (Html.BeginForm("AddMessage", "ForumController"))
{
<div>
@Html.LabelFor(m => m.NewMessage.MessageComposer)
</div>
<div>
@Html.TextBoxFor(m => m.NewMessage.MessageComposer)
@Html.ValidationMessageFor(m => m.NewMessage.MessageComposer)
</div>
<p>
<input type="submit" value="Submit new message" />
</p>
}
You can then use a Binding Prefix on your controller ...
public ViewResult AddMessage([Bind(Prefix="NewMessage")] NewMessage newmessage)
{
// logic to save
}
Upvotes: 1