开发者

after render Event on MVC. Possible?

开发者 https://www.devze.com 2023-02-24 08:25 出处:网络
Hy. I know that there are no server controls and server-side events, but...: My app is like an email box and the unread items are bold displayed.

Hy.

I know that there are no server controls and server-side events, but...:

My app is like an email box and the unread items are bold displayed.

after render Event on MVC. Possible?

Ok... the bold items are the unread (isRead==false). I want to update the item to (isRead=true) without any click.开发者_StackOverflow. just before the page is rendered.

How should I do it? Is there a way in ASP.NET MVC or will I have to do it with JQuery?

.

What is the best way to call a method AFTER "View" rendered?

.

Tks, guys!


If you are only trying to update the view, you can either change the model before passing it to the view, or you can use jQuery to modify the DOM after the page has been loaded into the browser.

On the other hand, if you are trying to change the model (and persist it as having been read), you can either update the model when you query for it before passing the model to the view, or you can use JavaScript (jQuery) to hit a link on your server telling you to mark those entries at read.

However, as R0MANARMY stated, just because someone has glanced at the title of your entry, does not mean that they have acknowledged it's existence.

This could be accomplished by opening a link to the individual entry to see the details of that entry, and have your logic mark the individual item as read at that time.

Hope this is helpful :)


Generally once your action has done it's thing, and you've made it to the view, there's no going back to code (so to speak). There are two ways to approach this problem.

  1. You create a copy of the list of messages to pass to the view and change the original to have isRead=true. I would say this is the preferred way of solving your problem and the code will look something along the lines of:

    var viewMessages = from m in messages  
                       select new Message  
                       {  
                            // Fields you want to copy go here   
                       }
    
    viewMessages.ToList(); //This actually creates a list from the query
    
    // update the isRead property
    foreach( var m in messages ){
         m.isRead = true;
    }
    

    You now have two collections, a copy of the original data you can pass to the view to render the data correctly, and the other is your original collection with all items updated with isRead = true.

  2. The other option isn't as good from the design point of view, but it will be able to accomplish the same goal. You can put the code to update the property directly into the view, right below where you do the check if it's read or not to change the bold. I'm assuming your view looks something along the lines of:

    @foreach(var message in View.Messages) {
       // some code
       <td @(message.isRead ? " class=\"selected\"" : null)
           message.Heading
           @{ message.isRead = true; } // Set it as read once it's been rendered
       </td>
    }
    
0

精彩评论

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