在 MVC 3 中的 DropDownList 中选择时采用 Null 值
我的控制器如下...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设 StudDept 是 StudentDetail 对象的外键(因此您要将所选值设置为 StudDeptId)。这将为您提供您定义的集合、值、标签和空字符串的默认值的下拉列表。
试试这个:
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:
我在 StudentModel 中创建了 getDept 的属性,并使用它填充了 Drodownlist。然后通过使用 Jquery 我传递了 Value it 。这是代码。
在控制器中我包括以下内容..
并在视图中..
最后我从 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.
And In Controller I included the Following..
And In View..
Finally I got the Selected value from Dropdownlist .
Hope it will be useful to others ...
Thanks