Click here to Skip to main content
15,885,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following code , in model I have the string "degree" is available to take the value which the user will select from the dropdown list, I want to assign the selected item by the user to model.degree
want to make something like this
HTML
@Html.hiddenfor(model=>model.degree, new id = selected item by user to model.degree )
how can I write it ,

What I have tried:

HTML
the code in the view where I want to select the value is as follow
<html>
<script type="text/javascript">
function qualificationSelected(selectedIndex){
    document.getElementById('b').style.visibility = (selectedIndex >= 1) ? 'visible' : 'hidden';
    document.getElementById('m').style.visibility = (selectedIndex >= 2) ? 'visible' : 'hidden';
    document.getElementById('p').style.visibility = (selectedIndex == 3) ? 'visible' : 'hidden';
}
</script>
<body>
<form name="myform">
    <table>
        <tr>
        <td> 
            <select name="one" onchange="qualificationSelected(this.selectedIndex)">
                <option value="" selected="selected">Select...</option>
                <option value="1">Bachelore Degree</option>
                <option value="2">Master Degree</option>
                <option value="3">PhD</option>
            </select>
            <input type="file" id ="b" style="visibility:hidden;" />
            <input type="file" id ="m" style="visibility:hidden;" />
            <input type="file" id ="p" style="visibility:hidden;" />
        </td><pre lang="text">
Posted
Updated 11-Apr-18 2:23am
v4
Comments
F-ES Sitecore 11-Apr-18 4:26am    
Rather than using "HiddenFor" use the DropDownListFor to make the dropdown as that function includes a parameter that says what model property the selected value goes into. Just google "mvc dropdownlistfor" and you'll find examples.
Member 2855047 11-Apr-18 8:22am    
I already have the script , just assigning the selected value to model directly ......
F-ES Sitecore 11-Apr-18 8:57am    
Change the select's name from "one" to "degree", and if you can't do that for some reason change your model's property from "degree" to "one".
Member 2855047 11-Apr-18 15:58pm    
it returns only id 1 2 3 , any idea how to assign the names
F-ES Sitecore 12-Apr-18 4:23am    
You're just going to have to write code that says "if id is 1 then text is Bachelore Degree".

Getting back to my original suggestion though, if you use DropDownListFor to make the select box you can use a server-side collection of items to build that list and that collection will have the id and the text. When you get the id back you can use the same collection to look up the text from the id.

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