surya karthi
surya karthi

Reputation: 51

Taking Null Value while Selecting in DropDownList in MVC 3

I am having the Controller as follows...

       public ActionResult Create()
    {

        ViewBag.Dept = (DbStudent.StudentDetails.Select(s => s.StudDept)).Distinct();
        return View();
    } 

    //
    // POST: /Student/Create

    [HttpPost]
    public ActionResult Create(StudentDetail studentdetail)
    {
        try
        {
            // TODO: Add insert logic here I used Stored procedure for it
            var query = DbStudent.StudInsert(studentdetail.StudName,
                                          studentdetail.StudDept, 
                           studentdetail.Mark1, studentdetail.Mark2);

            return RedirectToAction("Index");
        }

In View I included the following

  <div class="editor-field">
     @Html.DropDownListFor(model => model.StudDept, new SelectList(ViewBag.Dept as                       System.Collections.IEnumerable), 
       "Select Any Department")
  </div>

The DropDown get populated , But While Selecting the Dropdown I get only null values .. Please help me . I don't know what i am doing wrong

Thanks In Advance

Regards, Surya

Upvotes: 2

Views: 3238

Answers (2)

surya karthi
surya karthi

Reputation: 51

I have created a property of getDept in StudentModel and I populated the Drodownlist using it.Then by using Jquery i passed the Value it . Here is the code.

  public partial class StudentDetail
{   
    private string _StudName;
    private string _StudDept;
    private System.Nullable<int> _Mark1;
    private System.Nullable<int> _Mark2;
    private int _StudID;
    public StudentDetail()
    {
    }
    public IEnumerable<SelectListItem> getDept
    {
        get
        {
            StudentClassesDataContext objStud = new StudentClassesDataContext();
            var Dept = objStud.StudentDetails.Select(x => x.StudDept).Distinct();
            var deptlist = Dept.AsEnumerable();

            return deptlist.Select(x => new SelectListItem
            {
                Value = x.ToString(),
                Text = x.ToString()
            });

        }

    }....

And In Controller I included the Following..

 public ActionResult studentName(string Dept)
    {
        var result = from s in DbStudent.StudentDetails where s.StudDept == Dept                                         orderby s.StudName select s.StudName;
        var Studname = result.AsEnumerable().Select(x => new { value = x.ToString(),  text = x.ToString() });
        return Json(Studname,JsonRequestBehavior.AllowGet);
    }

And In View..

  <div>
  Search for Another Student<br />
    Department
     @Html.DropDownListFor(x => x.StudDept,new SelectList(Model.getDept, "Value",     "Text"),"--Select Any Dept --")
  Student Name
  @Html.DropDownListFor(x => x.StudName, Enumerable.Empty<SelectListItem>(), "--Select   Any Student--")

     <script type="text/javascript">
$('#StudDept').change(function () {
    var selectedDept = $(this).val();
    if (selectedDept != null && selectedDept != '') {
        $.getJSON('@Url.Action("studentName")', { Dept: selectedDept }, function (names) {
            var nameSelect = $('#StudName');
            nameSelect.empty();
            $.each(names, function (index, name) {
                nameSelect.append($('<option/>', {
                    value: name.value,
                    text: name.text
                }));
            });
        });
    }
});
     $('#StudName').change(function () {
    var Dept = $('#StudDept').val();
    var Name = $('#StudName').val();
    window.location = "/Student/Search/" + Name ;
    //alert("Selected Name is " + Name + " And Selected Dept is " + Dept);
});
  </script>

Finally I got the Selected value from Dropdownlist . Hope it will be useful to others ... Thanks

Upvotes: 1

Esteban
Esteban

Reputation: 2513

Assuming StudDept is a foreign key of the StudentDetail object (so you want to set the selected value to the StudDeptId). This should give you a dropdown for the collection you define, the value, label, and default value of an empty string.

Try this:

<%= Html.DropDownListFor(model => model.StudDeptId, 
                              new SelectList(ViewBag.Dept, 
                                             "id", 
                                             "Name", 
                                             ""))%>

Upvotes: 1

Related Questions