[英]asp.net mvc multiple button form
如何将这部分代码
<ol>
@foreach (var role in ViewBag.RolesForThisUser)
{
<li>@role <input type="hidden" name="DeleteRoleName" value="@role" /><input type="submit" value="Delete" name="action:DeleteRole" /></li>
}
</ol>
发布我需要控制器。 现在,它在列表上发布了第一个角色(无论我单击哪个按钮)。 通常我为每个列表选项执行多种形式,但是这次在其他形式中使用该列表。 我可以将表格放入表格吗?
一切都采用一种发布形式:
@using (Html.BeginForm("GetUserRoles", "Manage"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<p>
Users: @Html.DropDownList("UserName", (IEnumerable<SelectListItem>)ViewBag.Users, "Select ...")
<input type="submit" value="Get roles" name="action:GetUserRoles" />
</p>
if (ViewBag.RolesForThisUser != null)
{
<hr />
<p>
<text> Role name: </text>@Html.DropDownList("RoleName", (IEnumerable<SelectListItem>)ViewBag.Roles, "Select ...")
<input type="submit" value="Add role" name="action:AddRole" />
</p>
<hr/>
<p>
<text>User roles:</text>
<ol>
@foreach (var role in ViewBag.RolesForThisUser)
{
<li>@role <input type="hidden" name="DeleteRoleName" value="@role" /><input type="submit" value="Delete" name="action:DeleteRole" /></li>
}
</ol>
</p>
}
if (ViewBag.Action != null)
{
<hr />
<p>
@ViewBag.Action
</p>
}
}
请帮忙。 谢谢托马斯
您不能嵌套表单,最好使用ajax发布到控制器操作,该操作具有将角色添加为表单数据或参数所需的信息。 在开始表单标签中设置了提交操作,并手动设置了参数。 您还可以使用javascript基于单击按钮来更改提交时的表单属性,但是使用标准输入(使用要提交的每一行的数据字段触发ajax调用)会更容易遵循。 form.onSubmit属性将允许您将参数添加到表单操作中的同一操作。 button.onClick属性允许您构造Ajax调用,该调用将转到特定的控制器和操作来处理您的逻辑。
问题以不同的方式解决(3种形式-在ViewBag中传递UserName):
@using (Html.BeginForm("GetUserRoles", "Manage"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<p>
Users: @Html.DropDownList("UserName", (IEnumerable<SelectListItem>)ViewBag.Users, "Select ...")
<input type="submit" value="Get roles" name="action:GetUserRoles" />
</p>
}
@if (ViewBag.RolesForThisUser != null)
{
<hr/>
<p>
<text>User roles:</text>
<ol>
@foreach (var role in ViewBag.RolesForThisUser)
{
using (Html.BeginForm("GetUserRoles", "Manage"))
{
@Html.AntiForgeryToken()
<li>@role
<input type="hidden" name="UserName" value="@ViewBag.User" />
<input type="hidden" name="DeleteRoleName" value="@role" />
<input type="submit" value="Delete" name="action:DeleteRole" /></li>
}
}
</ol>
</p>
}
@using (Html.BeginForm("GetUserRoles", "Manage"))
{
@Html.AntiForgeryToken()
if (ViewBag.RolesForThisUser != null)
{
<p>
<text> Role name: </text>
@Html.DropDownList("RoleName", (IEnumerable<SelectListItem>)ViewBag.Roles, "Select ...")
<input type="hidden" name="UserName" value="@ViewBag.User" />
<input type="submit" value="Add role" name="action:AddRole"/>
</p>
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.