Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
this is my early tries with MVC. I'm trying to make search function by user id, the result of the search is a checkboxes list with all the permissions and departments .the ones that the user has access to are by default checked.
My problem now is when I try to choose other checkboxes, for example, the save button goes to the index function again and never goes to the saveData function.
I'm really lost and changed the code a lot. what should I do?

my controller

C#
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();
       }


What I have tried:

I tried to specify the action to the save button but still doesn't work as i want
@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>

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

    <form action="~/Controllers/Access/SaveData">
        <input type="submit" value="Save" class="btn btn-default" onclick="onClick" />
    </form>
    


}
<script>
    function onClick()
    {
        location.href = '@Url.Action("SaveData", "Access")';
    }


</script>
Posted
Updated 20-Dec-19 6:07am

You can't nest forms in HTML. Change the target of your outer form, and remove the nested form.

Also, remove the onClick method, which simply throws away the form and makes a GET request for the SaveData action.
Razor
@using (Html.BeginForm("SaveData", "Access"))
{
    @Html.AntiForgeryToken()
    
    ...
    
    <input type="submit" value="Save" class="btn btn-default" />
}
 
Share this answer
 
I did this and it went to the post function. thank you.😍
 
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