why property Speak Stuff give me error when not detected by session on asp.net mvc ?

Ahmed Salah Abed Elaziz 390 Reputation points
2023-11-15T20:54:06.6766667+00:00

I work on asp.net MVC 5 .NET Framework . I face issue I can't get value of Property SpeakStuff when Page Load .

on object ResignationRequester property SpeakStuff represent by two check boxes Yes and No .

if SpeakStuff equal True then Yes checkbox will be checked

if SpeakStuff equal False then No checkbox will be checked

so How to do that when page load without user check depend on value on database .

my code as below :

<a onclick="submit();" class="btn btn-primary" style="min-width: 100px;margin-top:5px;
margin-left: 1300px;"><i class="glyphicon glyphicon-ok"></i> Approve </a>

java script function for submit
function submit() {
    console.log("check submit");
        var ResignationRequester = new Object();
ResignationRequester.SpeakStuff = document.getElementById('SpeakStuff').checked;
}
csharp main model i will submit

 public class ResignationRequester
  {

   public bool SpeakStuff { get; set; }
  }
controller action catched when update data and submit

[ValidateAntiForgeryToken]
public async Task<ActionResult> ApprovalIndex(ResignationRequester REQ)
{
}

on asp.net mvc view sometimes session not detected or catch property Model.SpeakStuff so it display null

@if (Session[BLL.Common.SessionKeys.RoleCode].ToString() == "LM" || Session[BLL.Common.SessionKeys.RoleCode].ToString() == "LDM" || Session[BLL.Common.SessionKeys.RoleCode].ToString() == "DM")
{
    <h2 style="font-weight:bold;">Questions For Manager Before Approving:</h2>
    if (Model.DirectManagerStatus == "Approve")
    {

  <div>
@Html.Label("Did You Meet/Speak to Staff", htmlAttributes: new { @class = "control-label col-md-5" })
  <div class="col-md-7">
      <input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="true" class="speak-stuff-checkbox" @(Model.SpeakStuff ? "checked" : "") />
      Yes
      &nbsp;&nbsp;
      <input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="false" class="speak-stuff-checkbox" @(!Model.SpeakStuff ? "checked" : "") />
      No
  </div>
</div>

 $('.speak-stuff-checkbox').on('change', function () {
     $('.speak-stuff-checkbox').not(this).prop('checked', false);
     var selectedValue = $(this).val();
     $('#SpeakStuff').val(selectedValue);
 });
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,483 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 65,211 Reputation points
    2023-11-15T20:59:44.5666667+00:00

    why aren't you using radio button, which would implement this with no code?


  2. XuDong Peng-MSFT 10,511 Reputation points Microsoft Vendor
    2023-11-16T06:39:51.65+00:00

    Hi @Ahmed Salah Abed Elaziz,

    Based on the code you provided, I created simple demo but not reproduce your issue.

    on asp.net mvc view sometimes session not detected or catch property Model.SpeakStuff so it display null

    And you mentioned that the problem occurs intermittently, you can try clearing the browser cache and reopening your app to see if the problem still exists.

    Here is my test, and it worked fine. If the problem persists, please provide a minimal example that reproduces the problem.

    @if (Session["RoleCode"] != null && Session["RoleCode"].ToString() == "LM")
    {
        <div>
            <label>Did You Meet/Speak to Staff:</label>
            <div>
                <input type="radio" id="speakYes" name="SpeakStuff" value="true" @(Model.SpeakStuff ? "checked" : "") />
                <label for="speakYes">Yes</label>
     
                <input type="radio" id="speakNo" name="SpeakStuff" value="false" @(!Model.SpeakStuff ? "checked" : "") />
                <label for="speakNo">No</label>
            </div>
        </div>
    }
    else
    {
        …
    }
    
    public ActionResult MyActionMethod(int? id)
            {
                if (id == null)
                {
                    return RedirectToAction("Index");
                }
                var model = _context.ResignationRequesters.FirstOrDefault(r => r.Id == id);
     
                if (model == null)
                {
                    return HttpNotFound();
                }
     
                return View(model);
            }
     
        public class ResignationRequesterModel
        {
            public int Id { get; set; }
            public bool SpeakStuff { get; set; }
        }
     
        public class ApplicationDbContext : DbContext
        {
            public DbSet<ResignationRequesterModel> ResignationRequesters { get; set; }
          
        }
    

    Result:

    result

    Best regards,

    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.