Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This code is in my Index View.

C#
<pre><tr>
                <td class="Label">
                    @Html.Label("*DropDown")
                </td>
                <td>

                   
                    @Html.DropDownList("DropDown",
                        new List<SelectListItem>
                        { new SelectListItem {Text = "Tina", Value="1"},
                          new SelectListItem {Text = "Mina", Value="2"},
                          new SelectListItem {Text = "Jina", Value="3"},
                          new SelectListItem {Text = "Pina", Value="4"}, 
                        }, "Select one", new { @class = "ddl", })
                </td>
            </tr>


            <tr>
                <td class="Label">
                    @Html.LabelFor(m => m.PPC)
                </td>
                <td>
                   @Html.Hidden("PPC", null, new { id = "ppc1"})
                   @Html.Label("PPC", new { id = "ppc" })
                </td>
            </tr>


basically i want to populate my label and hidden field on selection of the DropDown automatically.
And to populate my Label and hidden field i am using this jquery.

C#
$(document).ready(function () {

       $('#DropDown').change(function () {

           var valueOfDDl = $("#DropDown option:selected").text();


           if (valueOfDDl == 'Tina') {
               $("#ppc").text("1 MW");
               $("#ppc1").text("1 MW");
           }
           else if (valueOfDDl == 'Mina') {
               $("#ppc").text("1.5 MW");
               $("#ppc1").text("1.5 MW");
           }
           else if (valueOfDDl == 'Jina') {
               $("#ppc1").text("2 MW");
               $("#ppc").text("2 MW");
           }
           else if (valueOfDDl == 'Pina') {
               $("#ppc").text("2.5 MW");
               $("#ppc1").text("2.5 MW");
           }
       })


I am successfuly setting the value
but when i tried to access that value in controller xyz it is null

I have tried Request.Form["PPC"].Tostring();
i have even passed it to my controller like this

C#
public ActionMethod xyz(string PPC)
{
Viewbag.ppc = ppc;
}


I have do have a ppc field in my model so i also tried
C#
Viewbag.ppc = model.PPC;


But nothing seems to work.

*
One more thing i want the label to be empty but when i load my page it shows PPC as default value and if i put simple Html label validations is not working.
Posted
Updated 22-Jul-15 1:31am
v6
Comments
Philippe Mori 22-Jul-15 8:11am    
Use a debugger to inspect the request. Also, having same name for hidden field and label look suspicious.

1 solution

Hi,

Use FormCollection in parameter of controller:

C#
public ActionMethod xyz(FormCollection collection)
{
   Viewbag.ppc = collection.GetValue("ppc").AttemptedValue;
}
 
Share this answer
 
v2
Comments
lokopi.nagar 22-Jul-15 7:51am    
Thanks but not Working.

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