0

I have a view in my asp.net mvc app, this view need a condition for display (or not) some information.

Unfortunately my condition need to get an object from the session and check several conditions

For example

<body>
<sometag>
<....>
<%
var oS = HttpContext.Current.Session["key"];

if(oS.some && oS.other == "other" && oS.Propertie == varInThisPage.Propertie && etc){

if(){

   if(){

       //in any place of universe
       return true;

       }
   }
return false; // for other
}
 %>
</body>

The problem is that I have that checks this condition in various part of the view, and do not want to create a method in the model, I feel that assassinate MVC

I thinking create a method in <% %> tag, but not working

bool MyMethod(){
var oS = HttpContext.Current.Session[InfoWeb.Models.SessionObjects.ConstSession.RT_SESSION];
....
return condition;    
}

In <% Visual Studio show error expected {

When i run, show error in next line with C# code

<%: Html.ActionLink("Create New", "BG", "CVSD")%> <!-- this work before i create method -->

I use asp.net-mvc 2

4
  • You should put that in the controller. Commented Aug 13, 2013 at 15:00
  • Controller send only the POCO class to the view, this problem is only for view, in my case. When there is a way to invoke the method from the controller, please let me know, i am java ee dev very unlucky Commented Aug 13, 2013 at 15:04
  • Put the logic in the controller and make the result a property of the model. Commented Aug 13, 2013 at 15:08
  • This way you are violating technically as well architecturally the principles of MVC. Try using a ViewModel. Commented Aug 14, 2013 at 3:10

1 Answer 1

1

<% %> blocks can only contain statements.
(the code in the block is placed inside the generated function)

To add fields or methods to the generated class, use <script runat="server">...</script>.

Sign up to request clarification or add additional context in comments.

3 Comments

@ataravati: I know (see my comment on the question). This answer is not wrong. You may be confusing MVC with Razor)
What I meant by wrong is that it's not a good solution. The code should be inside a Controller, as you rightly said in your comment.
i agree with ataravatti, it should be on the controller, and call this via ajax or jquery asynchronously. but in any case, for whatever reason, slaks solution will work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.