Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to validate user password before some actions for instance approving task list. If user pass incorrect password in the form (modal window) I need to pass the validation error to the details view where the approve button is available.

I'm trying to use ViewBag.IncorrectPassword = "Password is not correct. Please try again"; But no data is passed from the modal form to the view.

The question is how to pass the validation message from the aprove action to the detail view? View bag and ViewData does not work this time. I don't want to use javascript or ajax. Thanks in advance for your help.

What I have tried:

<h4>@ViewBag.IncorrectPassword</h4>

 <!-- Button trigger modal -->
           <button type="button" class="btn btn-success" data-toggle="modal" data-target="#ApproveTask">
               Approve
           </button>

           <!-- Modal -->
           <div class="modal fade" id="ApproveTaskList" tabindex="-1" role="dialog" aria-labelledby="ApproveTaskLabel" aria-hidden="true">
               <div class="modal-dialog" role="document">
                   <div class="modal-content">
                       <div class="modal-header">

                       </div>
                       <form asp-action="ApproveTask" asp-controller="TaskList" style="display:inline;">
                           <div class="modal-body">
                               <h5 class="modal-title" id="ApproveTaskLabel">Please enter your password</h5>
                               <br />
                               <input type="Password" class="form-control" name="Password" />
                               <br />
                               <span id="password_status" class="text-danger"></span>
                           </div>
                           <div class="modal-footer">
                               <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                               <input type="hidden" asp-for="TaskListID" />
                               <input type="hidden" name="id" value="@Model.TaskListID" />
                               <span asp-validation-for="TaskListID"></span>
                               <button type="submit" class="btn btn-success" id="approvebutton">Approve</button>
                           </div>
                </form>
                   </div>
               </div>
           </div>


public async Task<IActionResult> ApproveTaskList(int id, string Password)
        {
            var mtl = await _context.MyTaskList.SingleOrDefaultAsync(m => m.MyTaskListID == id);


                        if (await _userManager.CheckPasswordAsync(await _userManager.GetUserAsync(User), Password))
                        {
                            mtl.Status = MyTaskListStatus.Approved;
                            mtl.LastModifiedDate = DateTime.Now;
                             _context.MyTaskList.Update(mtl);
                            await _context.SaveChangesAsync();
                         }
                         else
                         {
                           ViewBag.IncorrectPassword = "Password is not correct. Please try again";
                         }

            return RedirectToAction("Details", new { id = mytasklistID });

}
Posted

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