0

I, have the partial view

@model BoardsXpress.Models.UserRoleAssignmentViewModels
@{
    var meetingTypeId = ViewBag.meetingTypeId;
    var meetingType = ViewBag.meetingType;}

<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="addUSerModalLabel">Assign Roles</h4>
    </div>
    @using (Html.BeginForm("PostUserAssignRole", "UserRole", FormMethod.Post))
            {
        @Html.TextBoxFor(m => m.MeetingTypeId , new { @type = "hidden", @Value= "@meetingTypeId" })
        <div class="modal-body">
            <div class="row form-group">

                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th scope="col">Users</th>
                            <th scope="col">Role</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model.userListViewModel)
                        {
                            <tr>
                                <td>@Html.CheckBoxFor(m => m.userListViewModel.First(x => x.UserId == item.UserId).SelectedUser, new { @class = "form-check-input" })&nbsp;&nbsp;@item.FirstName</td>
                                <td>@Html.DropDownListFor(m => m.userListViewModel.First(n => n.UserId == item.UserId).SelectedRoleId, item.RolesConvert, "Select Roles", new { @class = "form-control" })</td>
                            </tr>
                        }
                    </tbody>
                </table>

            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" id="close" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary">Done</button>
        </div>
    }
</div>

The partial view is call from the parent view

Here is the structure of the parent view

    @{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_DashboardSidebar.cshtml";
    var meetingTypeId = ViewBag.meetingTypeId;
    var meetingType = ViewBag.meetingType;
}

<div id="rightdash-container">
<button data-toggle="modal" data-target="#addUserRolesModal" class="btn btn-primary pull-right addButton">Assign User</button>
<div class="modal fade" id="addUserRolesModal" tabindex="-1" role="dialog" aria-labelledby="addUSerModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        @Html.Action("UserRoleManagement", "UserRole", new { id = @meetingTypeId })

    </div>
</div>
</div>

Controller method

public ActionResult PostUserAssignRole(UserRoleAssignmentViewModels model)
        {
            return RedirectToAction("usertest", "UserManagement", new { @id = TempData["MeetingTypeId"] });
        }

I am getting the default value in the model. I can see the data is reflected to the UI. However on post the database not been pass to the controller method.

Model class

    public class UserRoleAssignmentViewModels
    {

    public List<UserManagementViewModels> userListViewModel { get; set; }

        public Guid MeetingId { get; set; }
        public Guid MeetingTypeId { get; set; }
        public string MeetingType { get; set; }

    }

public class UserManagementViewModels
    {
        public string UserId { get; set; }
        public string Email { get; set; }
        public string PhoneNumber { get; set; }
        public string FirstName { get; set; }
        public string Organisation { get; set; }
        public string Designation { get; set; }
        public string Avatar { get; set; }
        public string Password { get; set; }
        public bool SelectedUser { get; set; }
        public string SelectTitle { get; set; }
}

The modal data is binded to the UI however the post method doesnot send the data to the controller.

San Jaisy
  • 15,327
  • 34
  • 171
  • 290
  • You cannot use a `foreach` loop to generate form controls for a collection (see the dupe) but you have other issues as well - for example `MeetingTypeId` is type of `Guid`, but you set the value attribute to something which could not bind to a `Guid`. NEVER attempt to set the `value` attribute when using `HtmlHelper` methods. In your case use `@Html.HiddenFor(m => m.MeetingTypeId)` –  Nov 23 '17 at 02:24
  • And since your using view models, you should never be sung `ViewBag` –  Nov 23 '17 at 02:26
  • what should I use to generate Controller instead of for loop – San Jaisy Nov 23 '17 at 02:27
  • Read the dupe :) - a `for` loop or `EditorTemplate` for typeof `UserManagementViewModels` –  Nov 23 '17 at 02:28
  • Can you show me a example .please – San Jaisy Nov 23 '17 at 02:30
  • An example of what. I have duped it. READ IT! –  Nov 23 '17 at 02:31

0 Answers0