views:

42

answers:

4

I think I just need someone to show me the obvious.

I have a spark partial

_MessageItem.spark

that is used within a view

like so

<for each="var m in messageList">
  <MessageItem message="m"/>
</for>

the partial looks like this:

    <tr id="${message.MessageId}">
        <td >${message.CreateDate.ToString("M/d/yy h:mm")}</td>
        <td >
            <b>${message.Subject}</b>
        </td>
        <td >${message.FromUser.FullName}</td>
        <td >${message.ToUser.FullName}</td>
    </tr>
    <tr>
        <td/>
        <td colspan="3">${message.Body}</td>
    </tr>

works like a champ, except when I try and call the partial directly from an action like so:

public ActionResult GetMessage(Message message)
{
  return PartialView("MessageItem",message);
}

When I do the above I get

error CS0103: The name 'message' does not exist in the current context

So my current solution is to create a wrapper partial that feeds the MessageItem partial

like so: _ActionMessageItem.spark:

<MessageItem message="(Message)ViewData.Model"/>

So can someone state the obvious and tell me how to modify

1) Modify my MessageItem partial so whether being called from PartialView() or within a .spark file it will work

2) Tell me how I need to modify my Controller Action so it won't throw an exception

A: 

try to call the partial with the underscore and an anonymous object.

 ViewData["message"] = message;
 return PartialView("_MessageItem");
Mitch R.
Unfortunately it didn't work :( I get the same error
Jose
Alright, I think the edit above should work now.
Mitch R.
A: 

When you pass parameters PartialView, spark doesn't know anything about your parameter name, only the value that was passed in. So, it uses the name of the argument, which I believe it is model. Since your code is looking for the parameter message it throws an error. I think one solution might be to change to something like this:

<MessageItem model="(Message)ViewData.Model"/>

model may be cased as Model, you'll have to guess and check.

Aaron D
That doesn't work because when it's inside another view that will throw an exception because you're trying to use ViewData.Model with two different types.
Jose
I think you'll have to avoid using the <MessageItem /> syntax and instead use # Html.RenderPartial("MessageItem", m); That works and allows you to use it either inline or as an ActionResult without throwing compilation errors.
Aaron D
A: 
<viewdata model="Message" message="Message" />
<var msg="message ?? Model" />

Then use the msg variable instead of message (like, ${msg.Subject}, etc).

You may also have luck with adding single

<default message="Model" />

but the first way is the one I think will work.

queen3
the default tag is what saved the day. Thanks!
Jose
A: 

the problem is that when you call it from the action you're passing data as a Model, but when calling from another view you're passing the data as a parameter. you could only use your data as Model if the other view also shares the same Model object type.

otherwise, what I'd do is pass it in ViewData in your Action:

public ActionResult GetMessage(Message message)
{
    ViewData["message"] = message;
    return PartialView("MessageItem");
}
dave thieben