0

On this page, a user fills out a web form, and it gets added to the list when created. I want to filter the list so a logged in user will only see forms they made themselves.

I have some Razor code that runs a foreach loop through all available items, and I have Javascript that rips the currently logged in user's info.

is it possible to assign this data to a var in the razor code?

ex.

@{var user = getUser(); //want something like this }

@foreach (var item in Model) {


    //add check if item.name == user.name here

        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.name)
            </td>
           etc etc

<script> 
    function getUser() {$.getJSON('GetLoggedUserInfo', function(data) {
         return data;
     });
</script>

2 Answers 2

1

I do not think it is a good idea to have this logic in a view. View should be as 'dumb' as possible. You could make item filtering at data storage level or at least in controller:

return View(items.Where(x => x.name == user.name))
Sign up to request clarification or add additional context in comments.

2 Comments

Worked great! As a follow up question, can I pass a second parameter to the View? Because I want a user to to be able to see certain links only if they're above a certain user group, so sending a simple bool would be nice, but doesn't seem possible.
You can pass any amount of parameters. ViewData/ViewBag would be suitable for that: codeproject.com/Articles/476967/…
1

There is an easier way. You can get current user properties using: HttpContext.Current.User

Comments

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.