3

I have got two different tables. User and ProjectDetails. There are two different controllers as well to do CRUD operations on these tables. Now, I have a case where, in the User CREATE operation, I have to select the Project from the List of Projects in ProjectDetails. I tried the following:

In the user model, I created this line:

    public IEnumerable<ProjectDetail> ProjectDetail { get; set; }

And in the controller, in the create Action, I have added the following code:

    public ActionResult Create()
    {
        var model = new UserDetail
        {
            ProjectDetail = db1.ProjectDetails
        };
        return View(model);
    } 

And in the create view, I am trying to get the list of Project IDs as follows:

@Html.DropDownListFor( x => x.ProjectDetail, new SelectList(Model.ProjectDetail, "Project ID"))

However, wen i run, i get the number of lines (as equal to the number of projects) as

System.Data.Entity.DynamicProxies.ProjectDetails_F########### (Some numbers)..

Please can someone help me?

Regards, Hari

[EDIT] - I checked in the debug mode and found the following.. Tried attaching the image..

I drilled down that Proxy things and found ProjectID there. How can I get that?

1

3 Answers 3

4

You are using a wrong overload, use this instead:

@Html.DropDownListFor( x => x.ProjectDetail, 
    new SelectList(Model.ProjectDetail, "ProjectId","ProjectName"))
// where ProjectId is the unique identifier field of `ProjectDetail` 
// and `ProjectName` is the text you want to show in the dropdown

In your code you are not telling the html helper what properties to use for the datavalue and the datatext. The overload you use is the one where you tell the htmlhelper which value is selected.

Sign up to request clarification or add additional context in comments.

7 Comments

ROCKING!!!!!!!!!! Please now, could you tell me how to map this to the User field? I got the dropdown. I should map the value selected in the dropdown to the input field in User mode which is Project.
Do you have something like User.Project where Project has a field of Id? If yest then do x=>x.Project.Id, new Selectlist...
von v - Sorry one more doubt. This was easy in my create action. How could I possibly do this for Edit? public ActionResult Create() { var model = new UserDetail { ProjectDetail = db1.ProjectDetails }; return View(model); } Now, in the Edit - I have public ActionResult Edit(string id) { UserDetail userdetail = db.UserDetails.Find(id); return View(userdetail); }
You do the same thing you did for the add. After you query your object (Find(id)) do the userinstance.ProjectDetail = db1.ProjectDetails. Also, you might want to consider working with viewmodels, it's better than passing your entities/models around.
great. Sure.. Will check ViewModel approach.
|
3

You can do something like

var projection = db1.ProjectDetails.Select(t => new ProjectDetailsViewModel
            {
                Prop1 = t.Prop1,
                Prop2 = t.Prop2
            });

Comments

0

Can you try

public ActionResult Create()
    {
        var model = new UserDetail
        {
            ProjectDetail = db1.ProjectDetails.ToList()
        };
        return View(model);
    } 

3 Comments

Try like [email protected]( x => x.ProjectDetail, new SelectList(Model.ProjectDetail, "Project ID"))
sorry like this @Html.DropDownListFor( x => x.UserDetail.ProjectDetail, new SelectList(Model.UserDetail.ProjectDetail, "Project ID"))
Looks lik, I have to have a different overload. Please check von v answer. Its working.. :)

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.