Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
so my Get function works fine and I search for the user and get his access on things in the checkboxes list. the problem is when I choose another checkbox and click save the model passed to the post function with the search ID are nulls. I don't know why. are not the model passed from the view supposed to hold the values.

my controller:

public ActionResult Index(int? SearchId)
        {
            var MainVM = new MainViewModel();
            var usr_Dep = db.TBL_User_Dep_Access.Where(x => x.UserID == SearchId).Select(x => x.Dep_ID).ToList();
            var usr_Per = db.TBL_UserPermissions.Where(x => x.UserID == SearchId).Select(x => x.PermissionID).ToList();

            if (SearchId != null)
            {
                List<UserDepartmentViewModel> Udep = db.TBL_Department.Select(i => new UserDepartmentViewModel
                {
                    DepId = i.Department_ID,
                    DepName = i.Department_Name,
                    IsChecked = usr_Dep.Contains(i.Department_ID)

                }).ToList();

                List<UserPermissionViewModel> Uper = db.TBL_Permissions.Select(i => new UserPermissionViewModel
                {
                    PerId = i.PermissionID,
                    PerName = i.PermissionName,
                    IsChecked_ = usr_Per.Contains(i.PermissionID)

                }).ToList();

                MainVM.UserDepartments = Udep;
                MainVM.UserPermissions = Uper;
            }
               
          
            
            return View(MainVM);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SaveData (MainViewModel mainView , int? SearchId)
        {
            db.TBL_UserPermissions.RemoveRange(db.TBL_UserPermissions.Where(x => x.UserID == SearchId));
            db.TBL_User_Dep_Access.RemoveRange(db.TBL_User_Dep_Access.Where(x => x.UserID == SearchId));
            foreach (var dep in mainView.UserDepartments)
            {
                if (dep.IsChecked)
                {
                    TBL_User_Dep_Access tBL_User_Dep_ = new TBL_User_Dep_Access();
                    tBL_User_Dep_.UserID = SearchId;
                    tBL_User_Dep_.Dep_ID = dep.DepId;
                    tBL_User_Dep_.IsActive = true;
                    db.TBL_User_Dep_Access.Add(tBL_User_Dep_);
                }
            }
            db.SaveChanges();

            foreach (var per in mainView.UserPermissions)
            {
                if (per.IsChecked_)
                {
                    TBL_UserPermissions tBL_UserPermissions = new TBL_UserPermissions();
                    tBL_UserPermissions.UserID = SearchId;
                    tBL_UserPermissions.PermissionID = per.PerId;
                    tBL_UserPermissions.IsActive = true;
                    db.TBL_UserPermissions.Add(tBL_UserPermissions);
                }
            }
            db.SaveChanges();
            return View();
        }


my view :
@using (Html.BeginForm("Index", "Access"))
{
    @Html.AntiForgeryToken()
    @Html.TextBox("SearchId", "", new { @id = "SearchId", @placeholder = "Search for...", @class = "form-control" })
    <span class="input-group-btn">
        <input class="btn btn-default" value="Search" type="submit">Go! />
    </span>
}
@using (Html.BeginForm("SaveData", "Access"))
{
    @Html.AntiForgeryToken()
        <ul>
            @if (Model.UserDepartments != null)
            {
                foreach (var item in Model.UserDepartments)
                {
                    <li>
                        <p>
                            @Html.CheckBoxFor(modelItem => item.IsChecked)
                            @Html.DisplayFor(modelItem => item.DepName)
                            @Html.HiddenFor(modelItem => item.DepId)

                        </p>

                    </li>

                }
            }

        </ul>


        <ul>
            @if (Model.UserPermissions != null)
            {
                foreach (var item in Model.UserPermissions)
                {
                    <li>
                        <p>
                            @Html.CheckBoxFor(modelItem => item.IsChecked_)
                            @Html.DisplayFor(modelItem => item.PerName)
                            @Html.HiddenFor(modelItem => item.PerId)

                        </p>

                    </li>

                }
            }

        </ul>


        <input type="submit" value="Save" class="btn btn-default" onclick="onClick" />
}


my viewmodel:
public class MainViewModel
   {
       public int SearchId { get; set; }
       public List<UserPermissionViewModel> UserPermissions { get; set; }
       public List<UserDepartmentViewModel> UserDepartments { get; set; }

   }


   public class UserDepartmentViewModel
   {
       public int DepId { get; set; }
       public string DepName { get; set; }
       public bool IsChecked { get; set; }

   }

   public class UserPermissionViewModel
   {
       public int PerId { get; set; }
       public string PerName { get; set; }
       public bool IsChecked_ { get; set; }


   }


What I have tried:

I can't put my hand on the issue here. so I don't know where to iterate to try it.
Posted
Updated 23-Dec-19 2:18am

The only values which will be sent to the action are the inputs within the form you've submitted.

The SearchId input is in a different form, so it won't be submitted to the SaveData action.

You'll need to add a hidden input to your SaveData form, and use JavaScript to copy the value from the visible input when the form is submitted.
Razor
@using (Html.BeginForm("SaveData", "Access", FormMethod.Post, new { id = "saveForm" }))
{
    @Html.AntiForgeryToken()
    <input type="hidden" name="SearchId" id="SaveData_SearchId" />
    
    ...
    
    <input type="submit" value="Save" class="btn btn-default" />
}

@section Scripts {
    <script>
    $("#saveForm").submit(function(){
        $("#SaveData_SearchId").val($("#SearchId").val());
    });
    </script>
}
 
Share this answer
 
Comments
Khloud Ali 20-Dec-19 13:55pm    
yeah, it worked but just for the SearchId part but what about the model. the viewModel contains the departments and permissions. All of those return to the post function as nulls.what am I doing wrong?
Richard Deeming 7-Jan-20 13:53pm    
You can't use a foreach loop to bind to a list of models; you have to use a for loop. For example:
<ul>
@if (Model.UserDepartments != null)
{
    for (int index = 0; index < Model.UserDepartments.Count; index++)
    {
        <li>
            @Html.CheckBoxFor(m => m.UserDepartments[index].IsChecked)
            @Html.DisplayFor(m => m.UserDepartments[index].DepName)
            @Html.HiddenFor(m => m.UserDepartments[index].DepId)
        </li>
    }
}
</ul>
if (SearchId != null)
            {
}
you are Assign Model List inside the Search not null
MainVM.UserDepartments = Udep;
               MainVM.UserPermissions = Uper;

So you are not get the
UserDepartments 
UserPermissions 
datas if Null of SearchId
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900