Search code examples
c#asp.net-mvcmultiple-forms

asp.net mvc multiple button form


How to make that part of code

        <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>

to post what i need to controller. Now it posts the very first role on the list (no matter which button i click). Usually i do multiple forms for each list option, but this time that list inside other form. Can i put form into form?

Everything is in one post form:

@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>
    }
}

Please help. Thanks, Thomas


Solution

  • Problem solved different way (3 forms - passing UserName in ViewBag):

    @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>
        }
    }