Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,
i am new in mvc

i am using mvc4
currently i learning mvc
i create a dropdown list and bind with databse.
for binding "firstnames" from databse , i am success,
i set firstname as a text
and StudentId as a Value
.
but i want to send dropdownlist selected values(id) from view to controller,
like example,
if i selected "john" then i want to get "john's id" in controller.
and i want to get all fileds(firstname,lastname,email,address) from id,

but i dont know how to do this

i tried to search on google but not success
.
please help me.

thanks in advance.
Posted
Comments
Samatha Reddy G 4-Oct-14 7:56am    
Can you please put your viewpage code?
Manish Dalwadi 4-Oct-14 8:15am    
for binding dropdownlist i wrote this code,
i used petapoco + my class file
controller
----------
public ActionResult Binddropdownlist()
{
Countries c1 = new Countries();

DataProvider dp = new DataProvider();

List<SelectListItem> selectitem = new List<SelectListItem>();
List<countries> items = (List<countries>)dp.getdata().Data;

foreach (var i in items)
{
selectitem.Add(new SelectListItem
{
Text = i.FirstName,
Value = Convert.ToString(i.StudentId)
});
}
ViewBag.fname = selectitem;

return View();
}
---------
View
---------
@model MVC_Controls.Models.Countries
@{
ViewBag.Title = "Binddropdownlist";
}


Binddropdownlist


@using (Html.BeginForm())
{
@Html.DropDownList("ddlfirstname", (IEnumerable<SelectListItem>)ViewBag.fname, "Select First Name")
<label id="fname">
dtd</label>

<input id="btnsend" type="button" value="Send" />
}
<script src="~/JS/jquery-2.1.1.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">

$("#ddlfirstname").change(function () {
debugger;
$.ajax({
url: "Home/getdata",
method: "POST",
data: "StudentId=" + $(this).val(),
dataType: "json",
success: function (response) {
window.location.reload();

// handle
}
});
});

1 solution

for binding dropdownlist i wrote this code,
i used petapoco + my class file
controller
----------
XML
public ActionResult Binddropdownlist()
       {
           Countries c1 = new Countries();

           DataProvider dp = new DataProvider();

           List<SelectListItem> selectitem = new List<SelectListItem>();
           List<Countries> items = (List<Countries>)dp.getdata().Data;

           foreach (var i in items)
           {
               selectitem.Add(new SelectListItem
               {
                   Text = i.FirstName,
                   Value = Convert.ToString(i.StudentId)
               });
           }
           ViewBag.fname = selectitem;

           return View();
       }

---------
View
---------
XML
@model MVC_Controls.Models.Countries
@{
    ViewBag.Title = "Binddropdownlist";
}
<h2>
    Binddropdownlist</h2>
@using (Html.BeginForm())
{
    @Html.DropDownList("ddlfirstname", (IEnumerable<SelectListItem>)ViewBag.fname, "Select First Name")
    <label id="fname">
        dtd</label>

    <input id="btnsend" type="button" value="Send" />
}
<script src="~/JS/jquery-2.1.1.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">

    $("#ddlfirstname").change(function () {
        debugger;
        $.ajax({
            url: "Home/getdata",
            method: "POST",
            data: "StudentId=" + $(this).val(),
            dataType: "json",
            success: function (response) {
                window.location.reload();

                // handle
            }
        });
    });
 
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