1

I want to reuse an combo box control which occurs in various modules in our project. Combo box with Select, Yes and no items in it.

@Html.DropDownList("SampleList", new[] 
{
new SelectListItem() {Text = "Yes",Value = "1" },
new SelectListItem() {Text = "No",Value = "2" }
},"Select")

What approach would be better?

To use Partial View or User Controls?

Also in certain forms they are repeated multiple time.

This is a questionnaire page.

Let me know if any further options are available as i am new to MVC. Thanks in advance.

2
  • Go for partial view, these type of thing we make and put it in a a folder named editor templates inside the shared folder. Also never heard of any thing like user controls in mvc Commented Oct 17, 2013 at 9:29
  • Hi Rex. But we see an optionw hile adding a new item MVC4 View User Control (ASPX) what would this do then? Commented Oct 17, 2013 at 9:37

1 Answer 1

2

To use Partial View or User Controls?

MVC doesn't have any notion of "user controls", partial views are pretty much the equivalent so they would be the most logical choice.


In a form if were to use it in 4 different places then i will be able to call using @Html.Partial but how do i differentiate between the four controls?

Just make your partial view take in the ID field e.g.

@model string

@Html.DropDownList(Model, new[] 
{
    new SelectListItem() {Text = "Yes",Value = "1" },
    new SelectListItem() {Text = "No",Value = "2" }
}, new { id = "@Model" })

Which you could then call like

@Html.Partial("SelectList", "ddlOne");
@Html.Partial("SelectList", "ddlTwo");
@Html.Partial("SelectList", "ddlThree");
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks James.. In a form if were to use it in 4 different places then i will be able to call using @Html.Partial but how do i differentiate between the four controls? What i mean is typically when we create any controls we provide an ID against which we will be validating. How to go about it in this scenario?
Thanks James. Let me try with those including the validation. :)
the ID's are not accessed by the jQuery. Here is a sample snippet from what you provided. $('#cmb1').change(function () { alert('First Alert Message'); }); cmb1 the id provided for the combo box is not detected. This change function never fires.
@GauthamNayak that isn't really much help... what does the HTML look like?
James. just take an example of a form which has questionnaire Are You? Yes/No Are You? Yes/No etc. We will be having around 15 questions so i was trying to create an partial view so that we can reuse it rather than creating it every time.
|

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.