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).
Related
I have run into a little roadblock in regards to joining mantle entities. I would like to have a single depicting fields from two mantle entities, but am unsuccessful in joining them. Specifically, I have linked a list of party relationships (as contacts) to a single partyId (vendor), with the goal to make a vendor contacts page. However I am unable to link that form-list with the PartyContactMech and ContactMech entities (in order to display email and phone number in the same form-list). More generally, my question is how can one map lists to each other the same way one can map a list to a single object (using entity-find-one and value-field does not work when tried with entity-find)?
There is no need to make a view-entity (join entities) to do that. Simply do a query on the PartyRelationship entity in the main 'actions' part of your screen specifying the toParty (vendor). Then in your Form-List, use 'row-actions' to query the PartyContactMech and so on for each fromPartyId (contact) entry that the previous query returned. Also have a look at the PartyViewEntities file in Mantle USL. There are some helpful view-enties already defined for you there such as PartyToAndRelationship, PartyFromAndRelationship etc. Also note that entity-find-one returns a single "map" (value-field) as it queries on the PK. Whereas entity-find returns a list of maps (list). They are separate query types. If I understand your question correctly.
I want to make an ASP.NET application in MVC 4. At first,i have two tables (Category & Barber) which have many to many relationship.For each Barber,there will be more than one categories and for every Barber, list of categories may be different and I want to give it to my model when I submit those from my "cshtml" and then i will do the rest.How can i do it?
I have a few models set up as Company and Employee. A company has many employees and a company should obviously be able to manage their employees.
What is the best approach to controller routing here? When a company want's to add a new employee should I route to the companies controller or to the employees controller?
For e.g. I could route to companies/:id/edit and then use a form with nested attributes to add employees. I could also just route to employees/new, or I could even use a nested resource and route to companies/:id/employees/new.
What do you think?
By the way I'm using STI, so Company < User and Employee < User
That's a good question. Here is what I think of the three options:
Routing to companies/:id/edit is unacceptable. Although you do edit a company by adding employees it is not meant for this purpose. This route should be used in order to edit company fields like company name, location etc.
Routing to employees/new is a good option, but not the one I would go on.
This is the best option in my opinion. In your case nested resources are created only in the context of their parent resource, so every aspect of you application should follow this. This includes the url for creating these nested resources, and the creation method itself which should be done through #company.employees.create!(params[:employee]) and not by Employee.create...
I think maybe it's not the best, but it's convenient, easy understand, and follow the rails way. You should use nested routes:
resources :companies do
resources :employees
end
Now you will have some routes like this:
company_employees GET /companies/:company_id/employees(.:format) employees#index
POST /companies/:company_id/employees(.:format) employees#create
new_company_employee GET /companies/:company_id/employees/new(.:format) employees#new
So, When you create a new employee, you know which company that employee belongs to thanks to :company_id. All codes for create new employee, update, destroy... of course, will be put in EmployeesController. Then you need to build a nested form for create new employees belong to one company.
If you want create, edit, update... company? Just create new resources for it:
resources :company
So, you can edit company, as in your question writen, through path: companies/:id/edit.
Scenario 1
In my web application say for there is a screen for adding an employee to system. As soon as user tabs after entering name of the employee, it generates the employee code automatically (which is the next field) based on some logic and already present records in the database.
Now I want to expose rest API for this application so that third party devs can build on top of it. So, I will have a resource called as /Employee which will respond for GET, PUT and DELETE verbs. But when a client needs to autofill the code, which is a GET operation, it will be on what resource? Should I make a new resource /EmployeeCodeFor/{Name} or I should get it on /Employee/{Name}/GenerateCode? If I go with /Employee/{Name}/GenerateCode then what about my resource to GET, PUT and DELETE for Employee i.e. actually /Employee/{Id}?
Scenario 2
Here lets take the case of a stackoverflow post. So lets say the resource would be /Post/{Id}. In the same way as in the previous example it lists me possible duplicate question as soon as I tab out of the Title field.
Again on what URL I should get those possible duplicates?
I can't think of more scenarios just now. But many such kind of scenarios may come up in real life application development. How to implement them in RESTful way?
Update on scenario 1
Code and Id are two different fields. Id is primary key, code can be duplicate across departments just to illustrate. Also to generate a code, name should be provided first. So, if user types a name "FirstName LastName" then server might generate FL003 as code assuming that there are already two more employees with firstname starting from F and lastname starting from L in the said department. Department can be identified based on the logged in user.
One way to allow the server an opportunity to pre-fill a bunch of elements in a new resource is to do
POST /Employees
{with empty body}
=>
201 Created
Location: http://example.org/employee/3443
<Employee Id="3443">
<Code>E1001</Code>
<FirstName></FirstName>
<LastName></LastName>
</Employee>
This gives the server one chance to provide default values. If you are looking for a more interactive way for the server to provide feedback during the input, I have another approach but it will take quite a bit more explaining.
Scenario 1
Let say your employee code is a unique identifier. In this case, to get it, you would allow the user to complete any field for the new employee and then make a POST. The server would generate the code and respond to the POST with a link to /Employee/{generated_code} which is the record for your newly created employee.
A GET on /Employee would return a list of all employees. A GET on /Employee/{a_code} will give you the employee detail.
Scenario 2
You could have some kind of query on the /Post collection like /Post?title_like={question_title}. A GET /Post?title_like=REST How to would return you a list of all questions containing "REST How to".
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.