C# asp.net mvc多按钮表单
如何使代码的这一部分C# asp.net mvc多按钮表单,c#,asp.net-mvc,multiple-forms,C#,Asp.net Mvc,Multiple Forms,如何使代码的这一部分 <ol> @foreach (var role in ViewBag.RolesForThisUser) { <li>@role <input type="hidden" name="DeleteRoleName" value="@role" /><input type="submit" value="Delete" name="acti
<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>
@foreach(ViewBag.RolesForThisUser中的var角色)
{
@角色
}
把我需要的东西贴到控制器上。现在它在列表中发布了第一个角色(无论我点击哪个按钮)。通常我会为每个列表选项创建多个表单,但这次是在其他表单中创建该列表。我能把表格变成表格吗
所有内容都在一个post表单中:
@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>
}
}
@使用(Html.BeginForm(“GetUserRoles”,“Manage”))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
用户:@Html.DropDownList(“用户名”,(IEnumerable)ViewBag.Users,“选择…”)
if(ViewBag.RolesForThisUser!=null)
{
角色名称:@Html.DropDownList(“RoleName”,(IEnumerable)ViewBag.Roles,“选择…”)
用户角色:
@foreach(ViewBag.RolesForThisUser中的var角色)
{
@角色
}
}
如果(ViewBag.Action!=null)
{
@取景袋,行动
}
}
请帮忙。
谢谢
Thomas如果不能嵌套表单,最好使用ajax发布到控制器操作,该操作包含将角色添加为表单数据或参数所需的信息。在期初表单标记中设置了提交操作,并手动设置了参数。您还可以使用javascript根据按钮单击来更改submit上的表单属性,但是使用标准输入触发ajax调用(使用要提交的每一行的数据字段)会更容易。 form.onSubmit属性将允许您向表单操作中的同一操作添加参数。
button.onClick属性允许您构造ajax调用,该调用将转到特定的控制器和操作来处理您的逻辑。如果不能嵌套表单,最好使用ajax向控制器操作发布,该操作包含将角色添加为表单数据或参数所需的信息。在期初表单标记中设置了提交操作,并手动设置了参数。您还可以使用javascript根据按钮单击来更改submit上的表单属性,但是使用标准输入触发ajax调用(使用要提交的每一行的数据字段)会更容易。 form.onSubmit属性将允许您向表单操作中的同一操作添加参数。
button.onClick属性允许您构造ajax调用,该调用将转到特定控制器并执行操作以处理您的逻辑。问题以不同的方式解决(3种形式-在ViewBag中传递用户名):
@使用(Html.BeginForm(“GetUserRoles”,“Manage”))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
用户:@Html.DropDownList(“用户名”,(IEnumerable)ViewBag.Users,“选择…”)
}
@if(ViewBag.RolesForThisUser!=null)
{
用户角色:
@foreach(ViewBag.RolesForThisUser中的var角色)
{
使用(Html.BeginForm(“GetUserRoles”,“Manage”))
{
@Html.AntiForgeryToken()
@角色
}
}
}
@使用(Html.BeginForm(“GetUserRoles”,“Manage”))
{
@Html.AntiForgeryToken()
if(ViewBag.RolesForThisUser!=null)
{
角色名称:
@DropDownList(“RoleName”,(IEnumerable)ViewBag.Roles,“选择…”)
}
}
问题以不同的方式解决(3种形式-在ViewBag中传递用户名):
@使用(Html.BeginForm(“GetUserRoles”,“Manage”))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
用户:@Html.DropDownList(“用户名”,(IEnumerable)ViewBag.Users,“选择…”)
}
@if(ViewBag.RolesForThisUser!=null)
{
用户角色:
@foreach(ViewBag.RolesForThisUser中的var角色)
{
使用(Html.BeginForm(“GetUserRoles”,“Manage”))
{
@Html.AntiForgeryToken()
@角色
}
}
}
@使用(Html.BeginForm(“GetUserRoles”,“Manage”))
{
@Html.AntiForgeryToken()
if(ViewBag.RolesForThisUser!=null)
{
角色名称:
@DropDownList(“RoleName”,(IEnumerable)ViewBag.Roles,“选择…”)
}
}
您是否有可能发布代码示例?Ajax代码,请问。您是否有可能发布代码示例?Ajax代码,谢谢。
@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>
}
}