Click here to Skip to main content
15,887,844 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, I am new using Razor MVC.

I have one partial view for dropdownlist.I already get display list for dropdownlist but for selected ID is 0. It should be have value.Below is my sourcode:

Main View:

HTML
<p>CUSTOMER INFORMATION</p>
        <table>
        <tr>
            <td><label for="customer">Customer</label></td>
            <td><div id="CustomerContainer">@Html.Action("CustomerView")</div></td>
            
        </tr>
        <tr>
            <td><label for="inCharge">Contact Person</label></td>
            <td><div id="InCharge">@Html.Action("CustomerDetailsView")</div></td> 
        </tr>



Partial View(CustomerView):
Choose Customer in this dropdownlist

C#
@Html.DropDownListFor(p=>p.SelectedCustomerID , Model.CustomerIEnum, "Select Customer", new{@id="Customer-DropdownID", @class="Customer-DropdownCls"})

<script type="text/javascript">

    $(function () {
        // Populate State/Provinces 
        $('#Customer-DropdownID').change(function () {
            var selectedCustomerID = this.value;
            $('#InCharge').load('@Url.Action("CustomerDetailsView")?customerID=' + selectedCustomerID);
             
        });
    });
</script>



Partial View(Customer Details View)
will Display Person In Charge.
@Html.DisplayFor(model => model.PIC)



Controller:

public static BusinessLogic.ViewModel.CustomerViewModel customervm = new BusinessLogic.ViewModel.CustomerViewModel();

C#
public ActionResult CustomerDetailsView(int? customerID)
        {using (BulkMailing2015Context db = new BulkMailing2015Context(connection))
            { if (customerID != null)
                {var q = db.Customers.Select(p => p.CustomerID == customerID);
                 customervm.SelectedCustomerID.CompareTo(q);
                }
            }
                return View(customervm);
        }



CustomerViewModel:

C#
public Int32 SelectedCustomerID { get; set; }
        public string PIC 
        {
            get
            {
                var q = from tbl in CustomerList where tbl.CustomerID == SelectedCustomerID select tbl.InCharge;

                return q.ToString();
            }
        }




Thanks in advance
Posted
Updated 24-Feb-16 17:18pm
v3
Comments
John C Rayan 1-Feb-16 10:16am    
Did you try

var selectedCustomerID = $(this).val();
heart1004 1-Feb-16 21:13pm    
Already try but didn't get value
John C Rayan 2-Feb-16 4:37am    
Can you put an alert and see if you get an object for #Customer-DropdownID
Can you check your HTML source using view source in browser for the dropdown id.

1 solution

Hi All,

A few week I have tried it,finally I got solution as below:

Main View:
HTML
<p><b>CUSTOMER INFORMATION</b></p>
        <div id="CustomerContainer">@Html.Action("CustomerView")</div>


Partial View:
HTML
<table>
<tr>
    <td><label for="Customer">Customer</label></td>
    <dd></dd>
    <td>@Html.DropDownListFor(p => p.SelectedCustomerID, Model.CustomerIEnum, "Select Customer", new { @id = "Customer-DropdownID" })</td>
    
</tr>

<tr><div id="InChargeContainer">@Html.Action("CustomerDetailsView")</div></tr>
</table>


JQuery:
HTML
<script type="text/javascript">
    $(document).ready(function () {
        $("#Customer-DropdownID").bind("change", function () {
            var ID = $(this).val();
            $.ajax({
                "type": "POST",
                "url": "/Sales/CustDetails/",
                "dataType": "json",
                "data": {"customerID": ID},
                "success": function (data) {
                location.reload();
                },
                error: alert('Customer ID is' + ID),
            });
        });
    });
</script>


Child Partial View:
HTML
<tr>
    <td><label for="inCharge">Contact Person</label></td>
    <td><dd>@Html.DisplayFor(model => model._selectedPIC)</dd></td>
</tr>


View Model:
C#
public List<Model.Models.Customer> CustomerList = new List<Model.Models.Customer>();

       public IEnumerable<SelectListItem> CustomerIEnum
       {
           get
           {
               return new SelectList(CustomerList, "CustomerID", "CustomerName");
           }
       }

       public Int32 SelectedCustomerID { get; set; }
       public string _selectedPIC { get; set; }



Controller:
C#
public JsonResult  CustDetails(int? customerID)
       {
           using (DBContext db = new DBContext(connection))
           {
               _custRepository = new GenericRepository<Customer>(db);

               if (customerID != null)
               {
                   IEnumerable<Customer> q = _custRepository.FindBy(p => p.CustomerID == customerID);

                   foreach (var item in q)
                   {
                       Customer obj = new Customer();
                       obj.CustomerID = item.CustomerID;
                       obj.InCharge = item.InCharge;

                       customervm.SelectedCustomerID = obj.CustomerID;
                       customervm._selectedPIC = obj.InCharge;
                       customervm.CustomerList.Add(obj);
                   }

               }

           }
           return Json (customervm);
       }
 
Share this answer
 

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