javascriptjqueryasp.net-mvcviewpage

Is it right to generate the javascript using C# in ASP.NET MVC view page?


[Sorry for long question but it is necessary to explain the problem]

I am working on a learning website and it is supposed to show a list of messages to user if there are any. Something like this:

alt text

When user presses close button, that message must be marked "read" and should not be shown next time. Following code is used to generate those messages:

<% foreach (var m in Model.UserMessages) { %>
    <div class="um" id="m<%=Html.AttributeEncode(m.ID) %>">
        <p class="mh"><%= Html.Encode (String.Format ("From {0} ({1})", m.From, m.Sent)) %></p>
        <p><%= Html.Encode (m.Text) %></p>
        <% using (Html.BeginForm ("CloseMessage", "Home", new { id = m.ID })) { %>
            <input type="submit" value="Close<%= Html.AttributeEncode (m.ID) %>" id="c<%= Html.AttributeEncode (m.ID) %>"/>
        <% } %>
    </div>
<% } %>

After that, following the guidelines, I added the support of http post method in controller which marks the message as read and then refreshes the view (to handle disabled JavaScript):

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CloseMessage (int id) {
    using (var dl = new DL ()) {
        dl.MarkAsRead (id);
    }

    if (Request.IsAjaxRequest ()) {
        return new EmptyResult ();
    }
    else {
        return RedirectToAction ("Index");
    }
}

Then I wanted to add JavaScript support such that only the message goes away (using jQuery). But the problem is that I am generating the buttons and messages programmatically.

So ended up with a weird looking javascript code in viewpage:

<script type="text/javascript">
    $().ready(function() {
        <% foreach (var m in Model.UserMessages) { %>
        $("#c<%=Html.AttributeEncode (m.ID) %>").click(function(event) {
            $.post("Home/CloseMessage/<%=Html.AttributeEncode (m.ID) %>");
            $("#m<%=Html.AttributeEncode (m.ID) %>").slideUp("slow");
            event.preventDefault();
        });
        <% } %>
    });
</script>

This is basically creating the javascript code in a C# loop which actually works but too much for me to digest. Is there any better way of doing this?


Solution

  • You could just create one javascript function that takes the element IDs as parameters :

    function myFunction(ID) {
        $.post("Home/CloseMessage/" + ID);
        $("#m" + ID).slideUp("slow");
    }
    

    And :

    <% foreach (var m in Model.UserMessages) { %>
    <div class="um" id="m<%=Html.AttributeEncode(m.ID) %>">
        <p class="mh"><%= Html.Encode (String.Format ("From {0} ({1})", m.From, m.Sent)) %></p>
        <p><%= Html.Encode (m.Text) %></p>
        <% using (Html.BeginForm ("CloseMessage", "Home", new { id = m.ID })) { %>
            <input type="submit" value="Close<%= Html.AttributeEncode (m.ID) %>" 
            id="c<%= Html.AttributeEncode (m.ID) %>" 
            onclick="myFunction('<%=Html.AttributeEncode(m.ID)%>')"/>
        <% } %>
    </div>
    <% } %>