locked
how to set select option value as "Y ' from controller to view Select dropdown RRS feed

  • Question

  • User-1355965324 posted

    I am trying to set the value 'Y' into a select list item from controller. Please can I ask your help to get sorted ? with Many Thanks

    I have the following model

    EmpDocumentModel
    {
    [NotMapped]
    public string IsClaim { get; set; }
    public string Claim { get; set; }
    }
    

    I have the controller as given below

    public IActionResult GetLicense(EmpDocumentModel data)
            {
                EmpDocumentModel doc = new EmpDocumentModel();
                data.Claim="Test Calim"
                if (data.Claim != null)
                {
                    data.IsClaim = "Y";  // I am trying to set the value in view only if data.claim is not null
                }
                
                return Json(data);
    
            }

    My view

     

    @model MyModel.Models.EmpDocumentModel
    <div class="col-md-6">
                                            <div class="form-group">
                                                <label asp-for="IsClaim" class="control-label control-label-left col-sm-3 text-danger">Any Claim?<br />[If Yes Give Details]</label>
                                                <div class="controls col-sm-9">
                                                    <div class="input-group">
                                                        <div class="input-group-addon">
                                                            <select id="IsClaim" name="IsClaim" asp-for="IsClaim" data-role="select" >  // I want to set the value as "Y' from controller, if  it is not form-control
                                                                <option value="N">No</option>
                                                                <option value="Y">Yes</option>
                                                            </select>
                                                        </div>
                                                        <input type="text" name="Claim" class="form-control" id="TxtClaim"  asp-for="Claim" data-role="text" disabled />
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
    
    <script>
      function SetLicense() {
        var url = '@Url.Action("GetLicense", "EmpDoc")';
        $.post(url, $('form').serialize(), function (view) {
            $("#TxtClaim").val(view.claim);
            $("#IsClaim").val(view.isclaim);  //is not working 
    
        });
        }
    </script>
    

    Thursday, March 12, 2020 9:25 AM

Answers

  • User475983607 posted

    Ridiculous!

    Same problem as your previous thread.  The case is incorrect, again, which indicates you are still not debugging your code.  Plus the code snippet you shared DOES NOT compile!

            [HttpPost]
            public IActionResult Index(EmpDocumentModel data)
            {
                EmpDocumentModel doc = new EmpDocumentModel();
                data.Claim = "Test Calim";
                if (data.Claim != null)
                {
                    data.IsClaim = "Y";  // I am trying to set the value in view only if data.claim is not null
                }
                else
                {
                    data.IsClaim = "N";
                }
    
                return Json(data);
            }
    function SetLicense() {
        var url = '@Url.Action("Index", "Home")';
        $.post(url, $('form').serialize(), function (view) {
            console.log(view);
    
            $("#TxtClaim").val(view.claim);
            $("#IsClaim").val(view.isClaim);  //is not working
        });
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 12, 2020 11:38 AM
  • User711641945 posted

    Hi polachan,

    The data called back from controller has two format:

    • If your property name is designed by one word like `Claim`,the default case for response is  lowercased letter 
    • If your property is composed of two words like `IsClaim`,it would response the camelcase by default.

    If you are not sure the return data's format,you could use the following code in your js:

    function SetLicense()
    {
        var url = '@Url.Action("GetLicense", "EmpDoc")';
        $.post(url, $('form').serialize(), function (view) {
            console.log(view);
            $("#TxtClaim").val(view.claim);
            $("#IsClaim").val(view.isClaim);
            });
    }

    Then you could press F12 in your view to check the response data:

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 13, 2020 3:07 AM

All replies

  • User475983607 posted

    Ridiculous!

    Same problem as your previous thread.  The case is incorrect, again, which indicates you are still not debugging your code.  Plus the code snippet you shared DOES NOT compile!

            [HttpPost]
            public IActionResult Index(EmpDocumentModel data)
            {
                EmpDocumentModel doc = new EmpDocumentModel();
                data.Claim = "Test Calim";
                if (data.Claim != null)
                {
                    data.IsClaim = "Y";  // I am trying to set the value in view only if data.claim is not null
                }
                else
                {
                    data.IsClaim = "N";
                }
    
                return Json(data);
            }
    function SetLicense() {
        var url = '@Url.Action("Index", "Home")';
        $.post(url, $('form').serialize(), function (view) {
            console.log(view);
    
            $("#TxtClaim").val(view.claim);
            $("#IsClaim").val(view.isClaim);  //is not working
        });
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 12, 2020 11:38 AM
  • User711641945 posted

    Hi polachan,

    The data called back from controller has two format:

    • If your property name is designed by one word like `Claim`,the default case for response is  lowercased letter 
    • If your property is composed of two words like `IsClaim`,it would response the camelcase by default.

    If you are not sure the return data's format,you could use the following code in your js:

    function SetLicense()
    {
        var url = '@Url.Action("GetLicense", "EmpDoc")';
        $.post(url, $('form').serialize(), function (view) {
            console.log(view);
            $("#TxtClaim").val(view.claim);
            $("#IsClaim").val(view.isClaim);
            });
    }

    Then you could press F12 in your view to check the response data:

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 13, 2020 3:07 AM
  • User-1355965324 posted

    Now I got the idea how to get the view elements .  Many Thanks also I am very sorry for the posting of  question which relatively seems to be simple. But when I doing the coding and if any  doubt I can only approach you for the help . Please forgive and give a help when get chance. Many Many thanks 

    Friday, March 13, 2020 6:51 AM