I am trying to get a list of linked users in to a MVC view & I am struggling!!!
Users table has an int ID and a varchar name
User Roles Table has int holderid, int officerid & int role id
Roles table has int id and varchar description
ID in users table is linked to both holder id & officer ID in user roles.
Role id is linked to id in roles
From my controller I am passing in one user ID to a strongly typed user role view. That view displays the name for the user id fine. Each user id will have multiple rows in user roles table, each with a different officer id.
My Issue
My issue lies in the fact that I cannot display the name from the users table that relates to an officer (or at least I can’t work out how to perform this in the view!! Or whether it should be done elsewhere in fact.)
Really what I want to do is say:
#Html.DisplayFor(modelItem => item.users.name.where(item.userid == item.officerid)
But this doesn’t seem to be working. I get this error: 'string' does not contain a definition for 'Where' and the best extension method overload
Can anyone offer any advice??
item.users.name is a string. And you cannot use where on it. Instead try something like this
item.users.Where(item.userid == item.officerid).FirstOrDefault().Name
Now here users is a table, hence a collection.
And are you working without Intellisense ?
Related
How do I store different sets of user information using Simple Memberships? For eg. I have two roles (doctors and patients) respectively. The extended user information to be stored for both the roles is somewhat different. Simple Membership creates a UserProfile table by default which I'd like to split into two tables to accomodate the user information for both the roles respectively.
Please suggest if and how this can be achieved.
Thanks.
You have Roles defined in your 'Roles' Table. Then, for a particular User, UserId and RoleId both are associated with each other in another Table named '_UserInRoles'. Now, Create another table maybe Named as 'UserAdditionalInfo' and
make table structure as below
Id(PK)
UserId(FK)
RoleId(FK)
Column1
Column2
Now, you can save user additional data according to its RoleId and UserId in another table and when you need to display User Profile on front end, then may be you can use JOIN in these tables and return information.
So I have a Customer model in my Rails app and whenever a new customer is created I am getting a number as input from the user and I want that number to become the id of the customer on my database instead of the default 1, 2, 3, and so on. Is it possible to do that? If so, how?
EDIT: Let me be a more clear about what I want: I have a Customer and A Brand model. A customer has_many brands and a brand belongs_to a customer. Whenever the user creates a new brand, I want to connect it to its customer using that number that the user entered for the customer. Right now, whenever the user creates a new brand, I ask him to enter the customer_id value to connect the brand to a customer, but that is the id number generated by Rails. How do I make that customer_id attribute refer to the specific number the user entered for a customer, instead of the customer id generated by Rails.
It's possible, but don't do that. Just allow Rails to manage the auto-generated ID itself, and add this number - whatever it is - as a separate attribute in your model and saved to a separate field in your database.
As smathy mentioned, you can do the following:
1.First, generate a migration that add your custom attribute
rails g migration AddCustomerIdToCustomers customer_id:integer
2.When user enter the id, you would want to do something like this:
#customer.customer_id=params[:customer_id]
#customer.save
3.Then you can retrieve the custom object as
#customer = Customer.find_by_customer_id(customer_id)
where customer_id is the id you want.
As the above answer suggests, it is better let Rails handle the id attribute, because it is used extensively in Rails to handle database relationships ActiveRecord queries, etc. You do not want the extra headache of modifying it yourself.
I am developing an ASP.NET MVC application. A user model has 4 properties:
First name
last name
Dob
Arabic name
And there are 4 members who will login. Depending on the login member I need to design the view with this 4 columns.
For example if member 1, I only want to show first name and last name.
If member 2, I need to show first name, last name and dob.
I achieved it by creating 4 different views and calling the views according to the member login. I don't think its good way of programming.
Can we store the detail in a sql table and call in view according to the member login?
For example a table with member login id and specify which are the column needs to display for him, then in view read this table and display the model values. Is this possible?
This is a role based job, so once you have created your roles for your different members you can simply add
[ReadOnlyAuthorize("A specific role")]
attribute above your field declaration
In order to minimize the number of joins that I have to execute in my application I decided to copy in my database the same field in several tables for example :
I have a User, Product and a Wishlist Table.
My Product pages shows the user who created the product, as the wishlists pages which also shows the user who created them.
So I added in my products and wishlists table all the users field needed to show the required informations.
How can I update the users related fields in my Products and Wishlists Table as soon as the user change his information ?
Here is a part of my model :
User Table
Full Name
UserName
Avatar URL
Product Table
Product Name
Product Price
User ID
User Full Name
Username
User Avatar URL
Wishlist Table
Wishlist Name
User ID
User Full Name
Username
User Avatar URL
Thanks in advance for your answers !
Firstly, by denormalizing the data in the way that you are, you are working against what relational databases are meant to do. They thrive on joins, and having each piece of data appear as few times as possible. So you really want to make sure that this is the schema that you want.
That being said, you can use the basic update_attibute syntax to update any field in any table. So, if a user edited his or her username, you would do:
User.update_attribute(:username, username)
Product.update_attribute(:username, username)
Wishlist.update_attribute(:username, username)
i have this code in my membership service class (taken from the asp.net-mvc sample app)
public MembershipUserCollection GetUnapprovedUsers()
{
MembershipUserCollection users = Membership.GetAllUsers();
MembershipUserCollection unapprovedUsers = new MembershipUserCollection();
foreach (MembershipUser u in users)
{
if (!u.IsApproved)
{
unapprovedUsers.Add(u);
}
}
return unapprovedUsers;
}
i now need a view to show this list of information and allow someone to approve them which will go back to the controller and set the IsApproved property to true.
Create a view which will generate a form containing label and checkbox for each member of the collection. You need to be able to get from the id of the checkbox to the user.
In the HTTP.POST Action method, iterate through the submitted fields looking for set checkboxes, when you find one set the corresponding user to approved.
Obviously the form can display arbitrary details for each user.
To use the inbuilt control helpers takes a bit more effort because you don't have a fixed size model to work with. To achieve something similar I:
Used a non-strongly typed view
populated ViewData["ids"] with IEnumerable<IdType> (which the view would loop over)
For each entry populated ViewData["field" + id] for each field I was displaying in the entity
In the view looped over the ids using ViewData["ids"] to call the HTML helpers with the id of the field.
(That was V1, in V2 I used model state so I could use the inbuilt validation error display support, but that doesn't really apply if you just want to select users.)
The POST processing was similar, repopulating the id list from the database and the looking up in the passed FormCollection.