Home Selecting one object from IEnumerable by clicking on a button
Reply: 0

Selecting one object from IEnumerable by clicking on a button

user3804
1#
user3804 Published in May 21, 2018, 12:49 pm

I'm creating a tool for editing/disabling Active Directory user accounts. I will search for a username and it will come up with all matches ("jsm" will come up with a table with "John Smith", "James Smoth", and "Jack Smuth"). Beside the user names are buttons "Edit" and "Disable". When the I click "Edit" or "Disable", a bootstrap modal will appear with textboxes containing the account properties such as SamAccountName and DisplayName inside of form inputs.

Because there could be multiple results, I'm passing in an IEnumerable into the view, then foreach through the @Model to put the data into the table. My next step is trying to pass in just that specific User object into the modal. So if the I click on the Edit button beside John Smith, I want his User object to populate the modal.

My first thought is doing a variable of User I can pass the User object from the foreach loop into then use that object in the modal, but I'm not sure how to go about doing that. Can/should I keep this inside the razor view or should I be passing this data into the controller then back out?

Controller:

namespace ADM.Controllers
{
    public class ManagementController : Controller
    {
        public new IActionResult User()
        {
            var user = new List<User>();
            return View(user);
        }

        [HttpPost]
        public new IActionResult User(string username)
        {
            var user = new User();
            var result = user.Get(username);

            return View(result);
        }

        public IActionResult Group()
        {
            return View();
        }
    }
}

User.cshtml:

@model IEnumerable<User>

@{
    ViewData["Title"] = "User Management";
    var count = 1;
}

<div class="container col-6">
    <div class="card card-square">
        <div class="card-body">
            <form asp-controller="Management" asp-action="User">
                <div class="form-row form-inline">
                    <input type="text" class="form-control col-10" name="username" placeholder="Username" />
                    <button type="submit" class="btn btn-primary">Submit</button>
                </div>
            </form>
        </div>
    </div>
</div>
<br />
@if (Model.Any())
{
    <div class="small">
        <table class="table table-hover table-sm">
            <thead>
                <tr>
                    <th scope="col"></th>
                    <th scope="col">Display Name</th>
                    <th scope="col">Description</th>
                    <th scope="col">SamAccountName</th>
                    <th scope="col"></th>
                </tr>
            </thead>
            <tbody>
                @foreach (var user in Model)
                {
                    <tr>
                        <th scope="row">@(count++)</th>
                        <td>@user.DisplayName</td>
                        <td>@user.Description</td>
                        <td>@user.SamAccountName</td>
                        <td>
                            <button id="userEditBtn" class="btn btn-sm btn-info" data-toggle="modal" data-target="#userEditModal">Edit</button>
                            <button class="btn btn-sm btn-danger" data-toggle="modal" data-target="#userDisableModal">Disable</button>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.321409 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO