views:

23

answers:

2

Controller action:

public string Index(){
var tmp = (from i in Message.message
                  where i.To == "bognjen" && i.Read == false
                  select i.Read).Count().ToString();
        return tmp;
}

Part of View:

<li><a>Inbox <%=Html.Action("Index","MyController")%></a></li>

When action Index return string it is not formatted with applyed style for <li>. How to format this string?

For example: action Index return number 4, then View page show: Inbox 4. Problem: Inbox have one style(font and size) and 4 have other style(with other font and size)

Style for <li> is:

li {
   list-style:none;
   background:url(images/bullet.png) no-repeat 0 5px;
   padding:3px 0 3px 17px;
}

This style should be for returned data from Index action (in my example that is 4)

A: 
var tmp = (from i in Message.message 
                  where i.To == "bognjen" && i.Read == false 
                  select i.Read).Count().ToString();

That should return a number. To format it, add a format string to the ToString()

Standard Numeric Format Strings

James Curran
+1  A: 

There are better ways to do this. For example:

Create this action method on some controller (let's call it MasterController):

[ChildActionOnly]
public ActionResult InboxStatus()
{
    var model = new InboxStatusViewModel();
    model.UnreadMessageCount = from i in Message.message
                   where i.To == "bognjen" && i.Read == false
                   select i.Read).Count();

    return PartialView(model);
}

InboxStatusViewModel is just a POCO.

Then create a partial view InboxStatus.ascx:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<InboxStatusViewModel>" %>

<li><a>Inbox (<%:Model.UnreadMessageCount %>)</a></li>

Then just call Html.RenderAction() in the view and specify you want InboxStatus action from MasterController. This will render the link and it will be styled and everything.

Necros