2

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

2 Answers 2

1

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", 
                                             ""))%>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your Immediate reply..StudDept is the Column of StudentDetails not a foreign key when i specify @Html.DropDownListFor(model => model.StudDept, new SelectList(ViewBag.Dept,"StudDept","StudDept",""), "Select Any Department") It Throws Error ..
I got Error -- 'System.String' does not contain a property with the name 'id'. While i changed it ..as @Html.DropDownListFor(model => model.StudDept, new SelectList(ViewBag.Dept,"id","Name",""))
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.