I need to send send value of particular row to controller method on clicking of anchor buttons. Also I need functionality to filter data based on dropdown selection(Working fine). I am newbie to asp mvc, Do not know if I am doing right if there is any BETTER solution without Jquery tables please suggest.
Here is my view structure:
@using (Html.BeginForm("Index", "Manage_Menu", FormMethod.Post, new { id = "myForm" }))
{<div style="float:left;padding-bottom:10px;">
<b>Select Parent Page</b>
<div>
@Html.DropDownList("ddlPageId", (IEnumerable<SelectListItem>)ViewBag.PageDDL, "Select parent page", new { onchange = "this.form.submit();" })
</div>
</div>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.PageName)
</th>
<th>
@Html.DisplayNameFor(model => model.IsActive)
</th>
<th>
@Html.DisplayNameFor(model => model.ShowInMenu)
</th>
<th>Move Menu Position</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.PageName)
</td>
<td>
<a href="javascript:void()" onclick="sumitForm()">
<input type="hidden" name="mmmm" value="@item.Id" />
@if (item.ShowInMenu == true)
{
<span class="glyphicon glyphicon-ok text-success" aria-hidden="true"></span>
}
else
{
<span class="glyphicon glyphicon-remove text-danger" aria-hidden="true"></span>
}
</a>
</td>
<td>
<a href="javascript:void()" onclick="sumitForm()">
@if (item.ShowInMenu == true)
{
<span class="glyphicon glyphicon-ok text-success" aria-hidden="true"></span>
}
else
{
<span class="glyphicon glyphicon-remove text-danger" aria-hidden="true"></span>
}
</a>
</td>
<td>
<a href="javascript:void()" onclick="sumitForm()">
<span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>
</a>
<a href="javascript:void()" onclick="sumitForm()">
<span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span>
</a>
</td>
</tr>
}
</table>
<script>
function sumitForm() {
document.getElementById("myForm").submit();
}
</script>
}
Here is my Controller:
public ActionResult Index()
{
var pages = db.PageMains.Where(a => a.ParentPageId == 0); ;
ViewBag.PageDDL = new SelectList(db.PageMains.Where(r => r.ParentPageId == 0), "Id", "PageName");
return View(pages);
}
[HttpPost]
public ActionResult Index(FormCollection aa)
{
if (!string.IsNullOrEmpty(aa["ddlPageId"]))
{
int filter = Convert.ToInt32(aa["ddlPageId"]);
var pages = db.PageMains.Where(a => a.ParentPageId == filter);
ViewBag.PageDDL = new SelectList(db.PageMains.Where(r => r.ParentPageId == 0), "Id", "PageName", filter);
return View(pages);
}
else
{
var pages = db.PageMains.Where(a => a.ParentPageId == 0); ;
ViewBag.PageDDL = new SelectList(db.PageMains.Where(r => r.ParentPageId == 0), "Id", "PageName");
return View(pages);
}
}
I have tried to store values inside hidden field but whenever I press any anchor button it is sending all the values.