1

I am new to mvc. I have done a fair bit of development with Winforms and now I am working on an MVC project. In our winforms project we have standard controls such a dropdown list control which will show all employees. This control knows how to get the employees and display them. It exposes a property which gives the selected employee. The forms just need to put this control on it and use it.

Now my question is how can I achieve this in ASP.NET MVC (in views). I tried partial views but then how can I expose the selected value to the parent view

1
  • 1
    Trying to do things in the same way as Winforms is going to be frustrating. The web is a very different beast. Try an embrace the differences rather than try to make MVC work like you are used to. Have a look at the videos on the right of the asp.net/mvc page. Commented Apr 16, 2013 at 14:17

3 Answers 3

1

I think this is what you're trying to achieve:

Say, in your Model, you have an Employee (or list of, either or):

Employee Employee { get; set; }

So, your main View should have this:

@Html.DisplayFor(Model => Model.Employee);

But, how do I template this, you ask?

You should take a step back at this stage and consider making a new ViewModel, an EmployeeViewModel (this, in your mind, should be your "partial view").

So now you'll have:

EmployeeViewModel Employee { get; set; }

Now, create a folder in your Views/Shared called DisplayTemplates and create EmployeeViewModel.cshtml. To be clear, you'll now have /Views/Shared/DisplayTemplates/EmployeeViewModel.cshtml.

The top line of this should read:

@model YourNamespace.EmployeeViewModel

And now you can simply use in this View:

@Html.LabelFor(model => model.EmployeeName) // or whatever properties you have

That should be sufficient enough to get you started.

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

Comments

1

In MVC, Views should not contains any logic other than display logic.

The Controller should be the one that "knows how to get the employees" or create an employee Model which will then pass to a View that knows how to display an employee, in most cases, it would be a strictly-typed View of type Employee.

Comments

0

PartialView will be fine to you. so search some content by the keyword 'PartialView'

Comments

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.