This is a migrated thread and some comments may be shown as answers.

Conditional statement within ClientTemplate

4 Answers 165 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Eugene
Top achievements
Rank 1
Eugene asked on 12 Mar 2012, 10:08 AM
Hi, i have problem with binding data which involves conditional statements in client template.
I have done this to column template, which will only trigger when server bind, but i do not have any idea how to do the same thing in client template so that when there is Ajax binding, it will also bind the same statement.

                      .Columns(columns =>
                              {
                                  columns.Bound(o => o.id).Template(
                                      o =>
                                      {
                                          if (o.status == "C" || o.status == "P")
                                          {
                                              if (HttpContext.Current.User.IsInRole(Admin)
                                              {
                                                  String.Format("<a href=\"{0}\">{1}</a>", URL + "/blog/" + o.id, "Preview");
                                              }
                                          }
                                 }

*edited, sorry, even Server binding with the above code, also don't work.. anyone can assist me for this?

4 Answers, 1 is accepted

Sort by
0
Dadv
Top achievements
Rank 1
answered on 12 Mar 2012, 02:18 PM
hi,

I think you should consider to transfer the security logic (admin role) to the controller.

Change your model to something like this :

public class MyModel
{
public int Id{get; set;}
public string Status {get; set;} 
public string Url { get;  set; }
}

and in you controller :

public Actionresult MyController()
{
List<MyModel> model = MyModelEntities();

model.ForEach(o=> if( (o.Status == "C" || o.Status == "P") &&  HttpContext.User.IsInRole("Admin")) {o.Url = Url.Content("~/[Your URL]/blog/"+o.Id)} );

return View(model);
}

in your View :

.Columns(columns =>
                              {
                                  columns.Bound(o => o.Url).ClientTemplate("<a href='<#= Url #>'>Preview</a>")
})


if you want that the empty url to be hide : 
on the grid :
.CllientEvent(e=>e.OnDataBound("onDataBound"))

script to add :
 <script type="text/javascript">
        function onDataBound(e) {
            $("#Grid a[href='']").hide();
        }
    </script>
0
Eugene
Top achievements
Rank 1
answered on 13 Mar 2012, 07:39 AM
hi, thanks for the tips. But, i afraid that wont be helping me, because there is quite a few if statement i need to do within ID column.

Is there anyway i can embed conditional statement within column template and client template?
0
Eugene
Top achievements
Rank 1
answered on 13 Mar 2012, 08:36 AM
Hi, i have successfully done it in column template with Razor, but i'm still stuck in client template where I can't check User's role,

any help?
0
Dadv
Top achievements
Rank 1
answered on 13 Mar 2012, 10:31 AM
As i know, you can't find any role information in client side (security behavior) and you shouldn't...

I don't understand why you don't want to use security logic in server side? do you have any technical constraints?

I'm probably wrong but the MVC pattern define that Views are only a presentation layer (read this) , all the logic should be in controller and/or the model. All the "logic" find in Views should be only "aesthetic".

Nevertheless, if you want to use conditional hide/show in client side you could do it by passing the Role value (and other needed stuff) in the ViewBag to the view, and use javascript to render the link... but it is a BIG security behavior...

It's a better way to adapt the Model to your View, this give you more flexibility and reduce the securities issues.
Tags
Grid
Asked by
Eugene
Top achievements
Rank 1
Answers by
Dadv
Top achievements
Rank 1
Eugene
Top achievements
Rank 1
Share this question
or