I am trying to create a Dropstrap Dropdown Button . The list should contain links that, when clicked, set the session state variable. The only page update I would like to do is display Dropdown again , so the selected item will appear as a title.
The dropdown is displayed as a partial view by calling a Child Action
public PartialViewResult SessionStateVariableSelector()
{
string stateVariable = Session["theStateVariable"] as string;
StateVariableCollectionViewModel model = GetThisFromSomewhere();
model.SelectedStateVariable = stateVariable ?? string.Empty;
return PartialView("_sessionStateVariableSelector", model);
}
At first, I had two ideas on how to do this.
A) Ajax.Beginformsurrounding Dropdown that sends the same action. Thus, I would do UpdateTargetIdthat updates Dropdown with a new variable selected.
, AutoPostBack Bootstrap. , <a> .
<a> href postback AJAX?
@using (Ajax.BeginForm("SetFund", new AjaxOptions { UpdateTargetId = "theSelector", HttpMethod = "Post" }))
{
<div class="row">
<div class="col-md-12">
<div class="btn-group pull-right" id="theSelector">
<button class="btn btn-default btn-lg dropdown-toggle" type="button" data-toggle="dropdown">
@if(string.IsNullOrEmpty( Model.SelectedStateVariable))
{
Please select....
}
else
{
<img src="@Url.Action("Image", "Resource", new { id = Model.SelectedStateVariable })" alt="image" class="img-responsive" />
<span class="caret"></span>
}
</button>
<ul class="dropdown-menu" role="menu">
@foreach (var item in Model)
{
<li>
@Html.HiddenFor(modelItem => item.Id)
<img src="@Url.Action("Image", "Resource", new { id = item.Id })" alt="@item.Name" class="img-responsive" />
</li>
}
</ul>
</div>
</div>
</div>
}
B) <a> href - , - . , .
<div class="row">
<div class="col-md-12">
<div class="btn-group pull-right" id="theSelector">
<button class="btn btn-default btn-lg dropdown-toggle" type="button" data-toggle="dropdown">
@if(string.IsNullOrEmpty( Model.SelectedStateVariable))
{
Please select....
}
else
{
<img src="@Url.Action("Image", "Resource", new { id = Model.SelectedStateVariable })" alt="image" class="img-responsive" />
<span class="caret"></span>
}
</button>
<ul class="dropdown-menu" role="menu">
@foreach (var item in Model)
{
<li>
<a href="@Url.Action("SetStateVariable", new { Id = item.Id })">
<img src="@Url.Action("Image", "Resource", new { id = item.Id })" alt="@item.Name" class="img-responsive" />
</a>
</li>
}
</ul>
</div>
</div>
</div>
ASP.NET MVC -.
, , - . , AngularJS, .