locked
Problem with Chosen Plugin When used with a DTO model View RRS feed

  • Question

  • User-1244692504 posted
                          <div class="col-md-6 col-sm-6 col-xs-12">
                                <div class="form-group">
                                    @Html.LabelFor(model => model.Underwriting_PolicyFile.BranchCode, htmlAttributes: new { @class = "control-label col-md-2" })
                                    <div class="col-md-10">
                                        @Html.DropDownList("Underwriting_PolicyFile.BranchCode", new SelectList(Html.BranchDropDownList(), "BranchCode", "Name", Model.Underwriting_PolicyFile.BranchCode), new { id = "Underwriting_PolicyFile.BranchCode", @class = "form-control input-sm" })
                                        @Html.ValidationMessageFor(model => model.Underwriting_PolicyFile.BranchCode, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>

    Please someone should help me with this plugin. It is not working with a DTO model stated below:

    public class ClientPolicyViewModel
        {
            public Underwriting_PolicyFile Underwriting_PolicyFile { get; set; }
            public IList<Underwriting_InsurerPolicyDistribution> Underwriting_InsurerPolicyDistribution { get; set; }
      
            public Underwriting_Policy_Code Underwriting_Policy_Code { get; set; }
            public ClientPolicyScheduleViewModel ClientPolicyScheduleViewModel { get; set; }
    
    }

    Underwriting_PolicyFile Model

    public class Underwriting_PolicyFile
        {
            public string PolicyRefCode { get; set; }
            public string BranchCode { get; set; }
            public string InsurerCode { get; set; }
            public string ClientCode { get; set; }
            public string InsBranchCode { get; set; }  
            public string PolicyCode { get; set; }
    
       }
    @Html.DropDownList("Underwriting_PolicyFile.BranchCode", new SelectList(Html.BranchDropDownList(), "BranchCode", "Name", Model.Underwriting_PolicyFile.BranchCode), new { id = "Underwriting_PolicyFile.BranchCode", @class = "form-control input-sm" })

    The Javascript is pasted below

                 $(document).ready(function () {
    
                        $(document).ready(ReadyMenu);
    
                        $("#Underwriting_PolicyFile.BranchCode").chosen({
                            disable_search_threshold: 5
                        });
                    });

    Please I will appreciate your advise to get this work.

    Please note that the chosen plugin works well with other models but only with a wrapped DTO models

    Saturday, January 5, 2019 6:53 PM

All replies

  • User475983607 posted

    A DTO model is irrelevant especially since you are populating a SelectList. 

    I assume the DTO is or whatever object you are using is not filled. 

    Please set a break point to  debug the following code.

    Html.BranchDropDownList()

    Saturday, January 5, 2019 8:09 PM
  • User-1244692504 posted

    Thank you mgebhard for your quick response please note that the Html.BranchDropDownList() works well, viewed through the debug mode as advised. What i intend doing  was  when SelectList is filled to pass the id to  id = "Underwriting_PolicyFile.BranchCode" model. However, when i changed the id to (id=BranchCode) in the view as stated below 

     @Html.DropDownList("Underwriting_PolicyFile.BranchCode", new SelectList(Html.BranchDropDownList(), "BranchCode", "Name", Model.Underwriting_PolicyFile.BranchCode), new { id = "BranchCode", @class = "form-control input-sm" })
    and on the the chosen javascript as shown below, it worked. 

                $(document).ready(function () {
    
                        $(document).ready(ReadyMenu);
    
                        $("#BranchCode").chosen({
                            disable_search_threshold: 5
                        });
                    });
    The problem now is how do i pass the id to  Model.Underwriting_PolicyFile.BranchCode. 
    It worked with the normal DropdownList. it is id issue with chosen plugin.

    Any idea on what to do get it working as id = "Underwriting_PolicyFile.BranchCode" will be greatly appreciated
    Thank you once again for your help.





    Sunday, January 6, 2019 6:21 PM
  • User1520731567 posted

    Hi Lawrence_Ajayi,

    Is Html.BranchDropDownList() your custom control?

    The problem now is how do i pass the id to Model.Underwriting_PolicyFile.BranchCode.

    @Html.DropDownList("Underwriting_PolicyFile.BranchCode", new SelectList(Html.BranchDropDownList(), "BranchCode", "Name", Model.Underwriting_PolicyFile.BranchCode), new { id = "BranchCode", @class = "form-control input-sm" })

    If you would like to pass id to Model.Underwriting_PolicyFile.BranchCode,I suggest you could use  @Html.DropDownListFor not  @Html.DropDownList.

    And Pass model from controller to view.

    For example:

    In View:

    @model xxx.Models.ClientPolicyViewModel//set strong typr model



    @Html.DropDownListFor(model => model.Underwriting_PolicyFile.BranchCode, new SelectList(Html.BranchDropDownList(), "BranchCode", "Name", Model.Underwriting_PolicyFile.BranchCode), new { id = "BranchCode", @class = "form-control input-sm" })

    In controller:

    you should pass the same type model in action:

     public ActionResult Edit(int Id,string str)
            {
    
                ClientPolicyViewModel model = new ClientPolicyViewModel();
    model.Underwriting_PolicyFile.BranchCode="111";//model must have a value of BranchCode, if not, add this line
    return View(model); }

    Best Regards.

    Yuki Tao

    Wednesday, January 9, 2019 10:14 AM