【发布时间】:2016-07-23 05:48:51
【问题描述】:
单击锚按钮时,我需要将特定行的发送值发送到控制器方法。我还需要根据下拉选择过滤数据的功能(工作正常)。我是asp mvc的新手,不知道我是否做得对,如果有没有Jquery表的更好的解决方案,请建议。
这是我的视图结构:
@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>
}
这是我的控制器:
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);
}
}
我尝试将值存储在隐藏字段中,但每当我按下任何锚按钮时,它都会发送所有值。
【问题讨论】:
-
把ID传到哪里?
-
您的视图没有意义,因为当您从下拉列表中选择一个选项时,表单正在提交。很抱歉很苛刻,但您所做的几乎所有事情都是不好的做法。向方法发送
ID的目的是什么? -
我需要更改数据库中 IsActive 和 ShowInMenu 的状态。
-
是的,这可能是不好的做法......对 mvc 来说是全新的......请指导
-
这是一个完全不同的过滤记录的操作(无论如何应该是 GET 而不是 POST)。您应该使用复选框并允许用户选中或取消选中任何或所有行中的任何
IsActive或ShowInMenu项目,并立即保存所有更改。
标签: asp.net-mvc