asp.net mvc multiple button form

601 Views Asked by At

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

2

There are 2 best solutions below

0
On BEST ANSWER

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>
    }
}
1
On

You can not nest forms, you would be better off using ajax to post to a controller action that has the information you need to add the role as form data or parameters. The submit action set in the opening form tag with the parameters manually set. You could also use javascript to change the form attributes on submit based upon the button click, but it would be easier to follow using standard inputs that trigger the ajax call using data fields for each row you are looking to submit. The form.onSubmit property will allow you to add parameters to the same action in the form action. A button.onClick property allows you to construct the ajax call that will go to a specific controller and action to handle your logic.