I'm new to MVC, currently running MVC 2, done a lot av regular ASP.Net in the past.
Background
Have a database containing all the tables for the .Net Membership provider.
Created a table called "Lists" containing UserID, ListID, ListName and so on.
It has a relationship key to the .Net Membership User, UserID column.
There is also a "List_Items" table that has ListID, ItemID, ItemName and so on.
What I have done
I managed to create a Entity Data Model mapping to the database. It contains both tables.
In the view I have done this:
<ul>
<% foreach (var item in Model) { %>
<li><%= Html.Encode(item.ListName) %></li>
<% } %>
</ul>
In the controller I have done this:
private ToDoDBEntities _db = new ToDoDBEntities();
return View(_db.Todo_Lists.ToList());
This I got from the asp.net/mvc tutorials. This tells me to things... I get all the Lists for all users. And I don't get the List Items from the other table.
What I want to do
I want to be able to pass the Provider Userkey to the database, for the logged on user, so I only get's his/hers lists.
I also would like to get Lists table and List Items table to the view in the same call so I can build the UL/IL with the List. And build content divs with the List Items.
Happy for any input.
/K
What tutorial are you using? Because I highly recommend http://www.asp.net/mvc as a learning resource, follow the tutorials and it will show you how to do what you want. It will also teach you techniques that will make life easier for you.
Related
I'm developing a data project with ASP.NET Core and Entity Framework Core. We're starting to use user claims (Identity) to manage authorization for things like managers etc. Previously, we had a separate table for managers, so that each permission could be set on each object and we looked it up as required.
Simple example prior to UserClaims:
Store { StoreId, Name etc... plus ICollection<ManagerPermissions> }
User { StoreId, Name, Email etc}
ManagerPermissions { StoreId, UserId }
Using the separate ManagerPermissions table meant it was easy to list the relevant manager(s) for that store by using a navigation property like ICollection<ManagerPermissions>. If writing a query, I could do _context.Store.Include(s => s.ManagerPermissions) to include a list of managers to display.
Now that we're using user claims, I haven't found an easy way to do this. I've extended IdentityDbContext so the claims data is held within the same database as the other data.
I have seen
UserManager.GetUsersWithClaimAsync({ ClaimType="ismanager", ClaimValue = StoreId})
but this requires an instance of userManager and also a separate roundtrip to the database. The only other way I could think of is by doing a join on UserClaims table, but I'm not sure the most efficient way of doing this.
I'm just getting into Yii. I have a simple relational db. I have a "client" table related to "orders" table (client:id to orders:client_id). If I build my CRUD for orders i naturally see client_id, however I'd rather add a lookup for client name somehow
I have found how to do this on new and update _forms by adding a dropDownList. The list based views seem a little more complex. I can see that actionIndex() in the controller is gathering data and passing it to index.php, finally through to _view, but I can't find any help on where and how I should break into this with my lookup returning the client name
I'd appreciate any help
thanks
Check the Yii documentation about relations. You will need to create a relation in your orders table, lets call it client, Then in your list view where it generates client_id you can put client.name instead. Now you will need to make sure that you have the appropriate label because in the generated model it will only have a label for client_id, and you need a label for client.name. Yii will guess, or you can add it, or you can modify the label for client_id, and instead of using client.name in the view you can use
array(
'name'=>'client_id',
'value'=>$model->client->name,
)
I tend to gravitate towards more explicit definitions rather than shortcuts, but to each his own.
I read this article.
My question is how can i get assigned courses for users in Index.cshtml ?
I add #Html.DisplayFor(modeltItem => item.Courses) to Index view and it shows the Ids of the courses correctly.
I want to see descriptions (or names) of courses.
I tried to use CourseDescription in UserProfileViewModel but I don't know how to use it in controller index action (in the article).
I have a model Comment, which has_a User.
In my display page, I am doing a Comment.all, and then displaying each comment.
In the view I need to display not only the comment, but also information about the associated user (ie the author).
<% #comments.each do |comment| %>
<%= comment.user.name %>
<%= comment.user.email %>
...etc...
<% end %>
This is fine and all, but activerecord translates this into one SELECT * FROM users WHERE USER.id = commentId query per EACH comment I have.
This is a little ridiculous, especially on a page with hundreds of comments. (That's hundreds of individual separate DB hits!)
When I am doing the Comment.all, is there away to tell rails to not only grab the comments, but also grab the associated users, and then when I call comment.user.blah later, for it to not grab it from db again? (This way it would do it all in one db statement).
You should use .includes.
From the doc:
Solution to N + 1 queries problem
Active Record lets you specify in advance all the associations that are going to be loaded. This is possible by specifying the includes method of the Model.find call. With includes, Active Record ensures that all of the specified associations are loaded using the minimum possible number of queries.
Revisiting the above case, we could rewrite Client.all to use eager load addresses:
clients = Client.includes(:address).limit(10)
clients.each do |client|
puts client.address.postcode
end
Or, in your case, it would be
Comment.includes(:user)
You can do this easily with ActiveRecord, you just need to use the eager loading feature in it. So Comment.all(:include => :user) will run one large query rather than one query per record.
Checkout http://stackoverflow.com/questions/29908230/rails-have-activerecord-grab-more-than-one-association-in-one-go/29908317#29908317 if you want to associate with more than one association in the includes statement.
The gist is using Comment.includes(:user, :dates, :whatevertableetc).
I am fairly new to Silverlight and RIA services, and I am trying to build a small project to understand it. So basically I created a Business Application, and I have the normal Login screen where I can add a user. That is fine and I can add a user and get him into the aspnet_Users table. Now I have created some extra fields, like Mob No, Tel No, DOB, Locality etc, which I have put in a table I have created called the UserProfile, and my idea is to get the username that was registered, and insert it into my UserProfile table with the other relevant data.
So I created a page called Profile.xaml, I created a UserProfileDomainService.cs where I have just one query, to get the user profile data from the table, and then created a Details DataGrid on my page, and the QueryName in my case is GetUserProfilesQuery(). Now what i wish to do is, get the user logged in, get his username, and check in my table to see if there is already data in the table. If there is populate the fields on the DataGrid with data, so that the user can modify this data, and if not, allow the user to insert data into the table.
So I have created a UserProfileViewModel class, and I want to create the query to get the data relevant to this user. However I got stuck on how to do all this, and how to get the user logged in.
Can anybody give me some advice or point me to some tutorials on how I can achieve this?
Thanks a lot and your help is very much appreciated.
In your domain service query you can use ServiceContext.User.Identity.Name to get the information specific to that user to include in your db query. I do something similar in our project.
We use entity framework so the LINQ to Entities query looks like:
return this.ObjectContext.UserSnapins
.Include("Snapin.EvolutionModule")
.Where(si => si.User.UserName == ServiceContext.User.Identity.Name)
.OrderBy(si => si.PageOrder);