下拉列表的问题 [英] problem with dropdownlist

查看:89
本文介绍了下拉列表的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有学生姓名下拉列表假设我想从下拉列表中选择名称,如果我在所选名称显示后单击添加按钮,请帮助我,我正在使用带剃刀的mvc。

I have student name dropdownlist Suppose I want to select name from dropdown if I click on add button after that selected name display as it is please help me I am using mvc with Razor.

<select  id="studentId" name="studentName" class="form-control" onchange="changestudent();">
                            <option value="0">select</option>
                           @{
              List<string> studentnames = student.DataLayer.Models.student.getAllstudentName();

                                  foreach (string studenttname in studentnames)
                                  {
                                      <option value="@studentname" >@studentname</option>
                                  }
                            }
                       </select></string>

推荐答案

您可以使用强类型视图并从btn点击发布活动返回模型。



例如: -

you can use strongly typed view and return the model from the btn click post event.

for example:-
[HttpPost]
public ActionResult Save(Student sdtudentModel)
{
//To your work
return View(studentModel)
}





然后在View中你可以使用代码绑定下拉列表



Then in View you can use the code to bind the dropdown List

@Html.DropDownListFor(model => model.StudentID, Model.StudentsList)





StudentID将是存储所选值的属性

StudentIDList将是IEnumerable< SelectListItem>这将存储所有下拉值。

这两个都将是你模型中的属性。



通过这个下拉列表将保留回发值(直到返回的模型具有StudentID的值)



StudentID will be the property that will store the selected Value
StudentIDList will be IEnumerable<SelectListItem> that will store all the dropdown values.
Both of these will the properties in your Model.

By this dropdown will retain value on postback(till the returned model has value for StudentID)


如果使用for循环绑定选项,则在changestudent()中调用webmethod将选定的值保存在ViewBag中并使用ViewBag显示所选值。
If you are using for loop to bind the options then in changestudent() call a webmethod to save the selected value in ViewBag and use ViewBag to show the value selected.


这篇关于下拉列表的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆