-1

I have controls that insert from jQuery. The controls can change depending on the option you choose before, that is, they are not controls have created statically, but to think through jQuery with the option you choose. My question is is possible to access a list of controller in HttpPost?

This is the function that performed to insert controls:

function GetProperties() {
        var subcategory = $("#ddlSubcategory").val();
        $.getJSON("/AddAdBox/GetProperties?id=" + subcategory, null, function (data) {
            $.each(data, function (i, item) {
                //item.ID // PROPERTY ID
                //item.NAME //NAME PROPERTY
                //item.OPTION_PROPERTY[0].ID //ID OPTION ID
                //item.OPTION_PROPERTY[0].NAME // NAME OPTION
                //item.OPTION_PROPERTY.length // SI ES 0 ES TXT SINO SELECT
                var itemlist = '';
                //<div class="col-sm-6">
                //                       <label>
                //                           Título
                //                       </label>
                //                       <input type="text" name="email" class="form-control" placeholder="Título">
                //                   </div><!-- /.col -->
                if (item.OPTION_PROPERTY != null) {
                    if (item.OPTION_PROPERTY.length > 0) {
                        itemlist = '<div class="col-sm-6 animated" style="opacity:1;"><label>' + item.NAME + '</label>';
                        itemlist += '<select id="ddl"' + item.NAME + '">';
                        for (var i = 0; i < item.OPTION_PROPERTY.length; i++) {
                            itemlist += '<option value="' + item.OPTION_PROPERTY[i].ID + '">' + item.OPTION_PROPERTY[i].NAME + '</option>';
                        }
                        itemlist += '</selec>';
                        itemlist += '</div>';
                    }
                }
                else {
                    itemlist = '<div class="col-sm-6 animated" style="opacity:1;"><label>' + item.NAME + '</label>';
                    itemlist += '<input type="text" name="' + item.NAME + '" class="form-control" placeholder="' + item.NAME + '">';
                    itemlist += '</div>';
                }
                $("#properties").append(itemlist);
            });
        });
    }
Victor GS
  • 3
  • 2
  • 1
    Not sure what you're asking; "to access a list of controller[s] in HttpPost" would imply a controller action (in C#), but you've pasted a whole ton of what appears to be irrelevant (to that question) javascript and the question refers to controls and "controllers" (which is an mvc term) in a question tagged with asp.net-mvc which have zero relationship to each other. Could just be a terminology issue. Did you mean list of controls? In which case you want `$(".form-control")` (all controls with class 'form-control') or `$("input")` (all inputs) – freedomn-m Aug 05 '15 at 20:34
  • I want to acces to the list of dinamic controls on the post [HttpPost] [ValidateAntiForgeryToken] Índice ActionResult público (string id) {return View (); } – Victor GS Aug 05 '15 at 20:38
  • Assuming they're in the `
    ` element then members of `HttpContext.Current.Request.Form` will give you all of the controls.
    – freedomn-m Aug 05 '15 at 23:14
  • Your dynamically created ` –  Aug 06 '15 at 04:13

1 Answers1

0

One way to bind data to a controller, from dynamic HTML elements, is by using a custom model binder. For example:

In my controller I use a viewmodel:

public class IndexViewModel
{
    public int SelectedItemId { get; set; }
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new IndexViewModel());
    }

    [HttpPost]
    public ActionResult Index(IndexViewModel viewModel)
    {
        return View(viewModel);
    }
}

I then create a custom model binder that can work with that viewmodel:

public class IndexViewModelBinder : DefaultModelBinder
{
    protected override void OnModelUpdated(ControllerContext controllerContext,
                                           ModelBindingContext bindingContext)
    {
        string[] allKeys = HttpContext.Current.Request.Form.AllKeys;

        var viewModel = bindingContext.Model as IndexViewModel;

        viewModel.SelectedItemId = int.Parse(HttpContext.Current.Request.Form[0]);
    }
}

In your Global.asax.cs file, place this in the Application_Start()

ModelBinders.Binders.Add(typeof(IndexViewModel), new IndexViewModelBinder());

In my view file I have:

@model SelectListModelBinding.Controllers.IndexViewModel
@{
    ViewBag.Title = "Home Page";
}

@using (Html.BeginForm())
{
    <div class="col-sm-6 animated" style="opacity:1;">
        <label>FooName</label>';
        <select name="ddlFooName" id="ddlFooName">
            <option value="1">Item 1</option>
            <option value="2">Item 2</option>
        </select>
    </div>
    <input type="submit" value="Save" />
}

You can pretend that the <select> was dynamically generated. Now when I post back to the controller, the IndexViewModelBinder is executed and I can see the values passed in the Form property of the current request. I can then pick out whatever data I need. In my example I'm picking out the id of the selected list item from ddlFooName

Since the names of the dropdowns will be dynamic, you may have to do some stuff with the Form.AllKeys to pick out stuff that starts with "ddl" and use that key to get the data.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154