在 MVC 3 中的 DropDownList 中选择时采用 Null 值

发布于 2025-01-01 18:42:59 字数 1032 浏览 0 评论 0原文

我的控制器如下...

       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");
        }

在视图中我包含了以下内容

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

填充了下拉菜单,但是在选择下拉菜单时我只得到空值.. 请帮我 。我不知道我做错了什么

提前致谢

, 苏里亚

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈场末日恋爱 2025-01-08 18:42:59

假设 StudDept 是 StudentDetail 对象的外键(因此您要将所选值设置为 StudDeptId)。这将为您提供您定义的集合、值、标签和空字符串的默认值的下拉列表。

试试这个:

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

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", 
                                             ""))%>
遗弃M 2025-01-08 18:42:59

我在 StudentModel 中创建了 getDept 的属性,并使用它填充了 Drodownlist。然后通过使用 Jquery 我传递了 Value it 。这是代码。

  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()
            });

        }

    }....

在控制器中我包括以下内容..

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

并在视图中..

  <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>

最后我从 Dropdownlist 中获得了 Selected 值。
希望它对其他人有用......
谢谢

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文