开发者

Spark View Engine Partial

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


<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.


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.


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

 ViewData["message"] = message;
 return PartialView("_MessageItem");


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");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜