Problems understanding the MVC - please help

F

Fritjolf

Hi.

I'm building myself a test application.
A part of it is just a guestbook. This guestbook displays a subject
field, body field and below that the posts that has been inserted to
the guestbook. It's build on linq to sql.

The model for the page lookes like this:
public class ParentViewModel
{
public Message message { get; set; }
//List of messages. Remember that only PARENT messages must be
filled in here
public TimelineViewModel timeline { get; set; }
}

where message is a linq object and TimelineViewModel is just a
IEnumerable<Message> .
The idea behind this is that when I call the view the message object
will be attached to the subject and body field in my UI (using the
editfor html helper) and the timeline will contain the messages
already posted to the page and display them.

The view lookes like this:

The aspx page lookes like this:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/
Site.Master"
Inherits="System.Web.Mvc.ViewPage<VollaSkoleClient.Models.ParentViewModel>"
%>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent"
runat="server">
Member
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent"
runat="server">
<% using (Html.BeginForm("Member", "Parent")) %>
<% { %>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Hva ønsker du å si</legend>
<p>
<%= Html.LabelFor(l => l.message.HeaderText) %><br />
<%= Html.EditorFor(l => l.message.HeaderText)%>
<%= Html.ValidationMessageFor(l => l.message.HeaderText)%>
</p>
<p>
<%= Html.LabelFor(l => l.message.BodyText) %><br />
<%= Html.EditorFor(l => l.message.BodyText)%>
<%= Html.ValidationMessageFor(l => l.message.BodyText)%>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<% Html.RenderPartial("Timeline", Model.timeline); %> </
asp:Content>


The controller for the first call (meaning the Get function) lookes
like this:
[Authorize(Roles = "Parent")]
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Member()
{
using (VollaSkoleDataContext dc = new VollaSkoleDataContext())
{
DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Message>(u => u.PortalUser);

dc.LoadOptions = options;

var model = new ParentViewModel
{
message = new Message(),
timeline = new TimelineViewModel { messages =
dc.GetParentMessages() }
};
return View(model);
}
}


And when I fill out some text in my subject and body and post back,
this triggers:
[Authorize(Roles = "Parent")]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Member(ParentViewModel model) //string header,
string body)
{
try
{
using (VollaSkoleDataContext dc = new VollaSkoleDataContext())
{
var message = new Message()
{
Category = "Parent",
UserId = dc.GetUserId(User.Identity.Name),
HeaderText = model.message.HeaderText,
BodyText = model.message.BodyText
};
if (ModelState.IsValid)
{
dc.Messages.InsertOnSubmit(message);
dc.SubmitChanges();
}
else
{
model.timeline = new TimelineViewModel { messages =
dc.GetParentMessages() };
return View(model);
}
}
}
catch
{
return View();
}
return RedirectToAction("Member", "Parent");
}


And it just doesn't work....
There are several problems. the model passed in is has the message
part of it with the new subject and body filled in, but the timeline
is null. Why? Secondly, I can't do a "ModelState.IsValid" at the top.
It always returns false. I suspect that is because the timeline object
is null and therefore the required fields in this object does not meet
the demands for my partial message class. (using data annotations on
this class). And if I try filling the timeline again (as shown above)
I get an exception when I come to the view with the error that the
datacontext is freed and cannot be accessed.

The thing I really can't seem to comprehend: for building a complexed
view I need a viewmodel. is it not correct that this view model should
contain both properties for updates (like message) and properties for
data to be displayed (like timeline) ???? If so, how do I post it back
to the controller? In my case I get the timeline which is null and
causes problems for me when I check ModelState.Isvalid - atleast I
suspect that to be the case...

I REALLY HOPE SOMEONE TAKES THE TIME TO READ AND TRY TO UNDERSTAND MY
PROBLEM AND HELP ME. I'M REALLY STUCK....

Please help me understand why it doesn't work...Or how to do this..

Thanx,
Fritjolf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top