vendredi 17 mars 2017

Disabling checkbox in ASP MVC based on bool property?

I have an MVC app where the user can register for classes, but I want to DISABLE checkboxes if the user has already registered for a class on the listView. Trying to disable a checkbox if the user has registered for that item already. The sessions users registered to are stored in a db.

        //Controller
        public ActionResult Index()
    {
        List<SessionViewModel> sessionVM;

        using (WSADDbContext context = new WSADDbContext())
        {
            sessionVM = context.Sessions
                .ToArray()
                .Select(x => new SessionViewModel(x))
                .ToList();
        }
        //TODO: Prevent user from registering to the same session
        return View(sessionVM);
    }

Not quite sure how to wire this property to a user, and check to see if he has this class in his cart.

  //Model
  public bool isRegistered { get; set; }

View

    @for(int i=0; i<Model.Count; i++)
    {
        var item = Model[i];
        <tr>
            <td>
                @Html.HiddenFor(modelItem => Model[i].SessionId)
                @Html.CheckBoxFor(modelItem => Model[i].isSelected)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SessionTitle)
            </td>
    }

I am NEW to MVC, and with some research, I was able to add the property to the view model, but there are multiple users and I need to be able to calculate who registered for what session and disable it on their list of available sessions. I want my output to look something like below:

enter image description here




Aucun commentaire:

Enregistrer un commentaire