I have this HTML code with a div whose contents I want to pass to the controller. The div basically contains some span items of the label-primary
class (link). I want to be able to get the names of the labels in this div in the controller. My idea was, as soon as the submit button is clicked, get the list of items that are inside the label-container
div and pass their names to the controller as the List<string> OwnerTerminals
value. How do I do that?
HTML + JS code:
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
...
<div id="register-terminal-panel" class="panel panel-default" style="max-width: 450px;">
<div class="panel-heading" style="color:#003F8B">
Add terminals to client
</div>
<div class="panel-body">
<div id="label-container"></div>
<div class="input-group input-group pull-right">
<span class="input-group-addon">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
</span>
<input id="register-terminals-search" type="text" class="form-control" placeholder="Search terminals" aria-describedby="search-addon" onkeyup="searchTerminals()">
</div>
</div>
<ul id="register-terminal-list-container" class="list-group">
@foreach (Point p in terminals)
{
<li id="list-item-@p.Name"><a href="javascript:void(0)" class="list-group-item" onclick="createLabel('@p.Name')">@p.Name</a></li>
}
</ul>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Register" />
</div>
</div>
}
<script>
function closeLabel(item) {
item.parentElement.remove();
var listItem = '\n<li id="list-item-' + item.parentElement.id + '"><a href="javascript:void(0)" class="list-group-item" onclick="createLabel(\'' +
item.parentElement.id + '\')">' + item.parentElement.id + '</a>';
$('#register-terminal-list-container').append(listItem);
sortUnorderedList("register-terminal-list-container");
};
function createLabel(name) {
var label =
'<span id="' + name + '" class="label label-primary terminal-label"><span class="glyphicon glyphicon-remove" onclick="closeLabel(this)" id="terminal-label-close-btn"></span> ' + name + '</span>\n'
$('#label-container').append(label);
$('#list-item-' + name).remove();
};
</script>
Model:
public class RegisterViewModel
{
...
public List<string> OwnerTerminals { get; set; }
}