0

I am trying to create controls dynamically in asp.net mvc4 but having some issues and need some suggestions as well. The following statement in the below is not working it shows me error line beneath "row.Options" @for (var o in row.Options )

Beside this, can i dynamically create dropdownlist or dropdownlistfor if yes then how can i assign name and values according to current scenario?

View

            @foreach(var row in Model.Controls) {
                var i = 0;
                <div>                        
                     <label>@row.Caption</label>
                     @if (row.ControlType == "text") {
                        <input type="text" name="@row.Name" />
                     } else if (row.ControlType == "select") {

                        <select name="@row.Name">
                            @for (var o in row.Options ) {
                                <option value="@o.Value>">@o.Text</option>
                            }
                        </select>                             
                     }                                          
                </div>  
                i++;
            }
            </ul>

View Model

public List<SelectListItem> MembershipTypeList { get; set; }

public class Controls
{
    public string Caption {get; set;}
    public string ControlType {get; set;} 
    public string Name {get; set;}
    public List<SelectListItem> Options { get; set; }
}

Controller Snippet

    public ActionResult Signup()
    {
        signup.MembershipTypeList.Add(new SelectListItem()
        {
            Text = "Student  £18.00",
            Value = "Student"
        });

        signup.MembershipTypeList.Add(new SelectListItem()
        {
            Text = "Associate  £18.00",
            Value = "Associate"
        });

        signup.Controls = new List<Controls>();
        signup.Controls.Add(new Controls() { Caption = "Age", ControlType = "text", Name = "txtage" });
        signup.Controls.Add(new Controls() { Caption = "Name", ControlType = "text", Name = "txtname" });
        signup.Controls.Add(new Controls() { Caption = "Membership", ControlType = "select", Name = "txtmembership", Options = signup.MembershipTypeList });

        return View("Signup", signup);
}

2 Answers 2

2

I think it might just be an extra angle bracket causing this, try changing to:

<option value="@o.Value">@o.Text</option>
Sign up to request clarification or add additional context in comments.

Comments

1

For starters, your code is invalid here:

<select name="@row.Name">
    @for (var o in row.Options ) {
        <option value="@o.Value>">@o.Text</option>
    }
</select> 

Remove the ">" from @o.Value>

Rendering as it is breaks the html.

Fix:

<select name="@row.Name">
    @foreach (var o in row.Options ) {
        <option value="@o.Value">@o.Text</option>
    }
</select> 

1 Comment

it was simple error i just had to put "@foreach" instead of @for... thanks guys

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.