开发者

How do I handle a filtered and unfiltered list in the same asp.net mvc view?

开发者 https://www.devze.com 2022-12-16 20:25 出处:网络
I need a way to display whether a user has subscribed to a service or not and to display it into a table in a view. So far I am able to show if that a user has subscribed to a service but am not able

I need a way to display whether a user has subscribed to a service or not and to display it into a table in a view. So far I am able to show if that a user has subscribed to a service but am not able to show when they have not subscribed.

I created a viewmodel like this:

public class MyServicesController : Controller {

    public ActionResult Index()
    {
        WillEntities we = new WillEntities();

        var servicegroups = we.ServiceGroupSet.ToList();
        var services = we.ClientServiceSet.Include("AspnetUser").ToList();
        var aspnetusers = we.AspnetUserSet.Include("ClientService").ToList();

        return View(new MyServicesViewModel (servicegroups, services, aspnetusers));
    }
}

Then rendered the services table to the view like this:

<div id="Services">
<h2>My Services</h2>   
<table>
<tr>         
<th class="th1">
Service Name
</th>
<th class="th3">
Select / Deselect
&l开发者_运维问答t;/th>
</tr>
</table>
<% foreach (var servgroup in Model.ServiceGroups )
{ %>
<ul> <%= servgroup.GroupName  %> </ul>     
<table>
<% foreach (var serv in servgroup.ClientService )
{ %>  
<tr>
<td class="td1">     
<%= serv.Description%>
</td>
<td class="td3">        
<%foreach (var user in serv.AspnetUser) 
{ %>
<%if (user.UserName.Equals(User.Identity.Name, >StringComparison.OrdinalIgnoreCase))
{ %>
Already subscribed             
<% }
else
{%>
<%} %> 
<%} %>     
</td>
</tr>        
<% } %>   
</table>          
<% } %>
</div>

Which works fine at pulling and filtering any users subscribed to that service then seeing if the current user is in that list.

But given that by filtering in this way by using the <%foreach (var user in serv.AspnetUser) clause I am pulling only from the list of users that have chosen a particular given service.

So I have no way at the moment to grab the unchosen services and to display the "Not yet subscribed" string to render into that service's row in the table.

I tried creating a boolean function to pull out the "user" var above and return a true or false of whether the current user has subscribed to that service:

public class MyServicesViewModel
{      

    public bool UsersServices(AspnetUser user)
    {
        if (user.UserId.ToString() == "35l1cob9-rest_of_user_guid-96975")
            return (true);
        else
            return (false);
    }
}

but could not figure out a way to get it into the view, and get the feeling I am overcomplicating it by trying this method.

Can anyone see a less complicated way to do this?

Thanks a lot,

Paul


It looks like you're using Entity Framework for your ORM? I'm more familiar with Linq to SQL, and it's difficult to give you an exact solution without knowing all the behind the scenes details - but assuming you have the appropriate primary/foreign key relationships setup in your db - you should be able to use linq to query the information you need:

<table>
<tr>
<td class="td1">     
<%= serv.Description%>
</td>
<td class="td3">        
<% if (serv.AspnetUser.Where(u => u.UserName == User.Identity.Name).Count() > 0) 
{ %>
Already subscribed
<% }
else 
{ %>
Not subscribed
<%
} %>
</td>
</tr>        
</table>

Hope that helps.

0

精彩评论

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