如何绑定MVC5中的dropdownlist控件的值 [英] How to bind values for dropdownlist control in MVC5

查看:175
本文介绍了如何绑定MVC5中的dropdownlist控件的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我只是想将值绑定到下拉列表控件。我看不到任何例子,但它对我不起作用。请找到我的代码和错误,并帮我解决这个问题。



//我的模特



public class SubjectModel

{



public int selectedid {get; set;}



public List< SelectListItem> ddllist {get; set;}



public SubjectModel()

{

ddllist = new List< SelectListItem> ;();

}

}



//我的控制器



public ActionResult DropDownTest()

{



SubjectModel model = new SubjectModel();

model.ddllist.Add(new SelectListItem {Text =Physics,Value =1});

model.ddllist.Add(new SelectListItem {Text =Chemistry,值=2});

model.ddllist.Add(new SelectListItem {Text =Mathematics,Value =3});



model.selectedid = 0;



返回查看(型号);

}

//我的观点



<!DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3。 org / TR / xhtml1 / DTD / xhtml1-transitional.dtd>

<%@ Page Language =C#Inherits =System.Web.Mvc.ViewPage< dopdownbinding.models.subjectmodel>%>



< html>

< head>

< meta charset =UTF-8 />

< meta name =viewportc />

< title> DropDownBinding< / title>

< / head>

< body>







< label for =timezone>主题< / label>



@ Html.DropDownListFor(model => model。 selectedid,new SelectList(Model.ddllist,SubCategoryId,SubCategoryName,Model.selectedid))





< / body>

< / html>





我收到这个执行此操作时出错。

表达式树可能不包含动态操作

我是ASP.Net MVC的新手,如果出现问题,请帮我纠正。谢谢< DIV class =h2_lin>解决方案

现在已经解决了。



我的下拉剃须刀代码应该是

@ Html.DropDownListFor(model => model.selectedid,Model.ddllist)



由于我的ddllist属性已经是List< selectlistitem>,它可以正常使用此代码。


Hi All, I am just trying to bind values to a dropdownlist control. I could see no of examples but it is not working for me. Please find my code and the error and help me to solve this.

//My model

public class SubjectModel
{

public int selectedid {get;set;}

public List<SelectListItem> ddllist{get;set;}

public SubjectModel()
{
ddllist = new List<SelectListItem>();
}
}

//My controller

public ActionResult DropDownTest()
{

SubjectModel model = new SubjectModel();
model.ddllist.Add(new SelectListItem { Text = "Physics", Value = "1" });
model.ddllist.Add(new SelectListItem { Text = "Chemistry", Value = "2" });
model.ddllist.Add(new SelectListItem { Text = "Mathematics", Value = "3" });

model.selectedid = 0;

return View(model);
}
//My View

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dopdownbinding.models.subjectmodel>"%>

<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" c/>
<title>DropDownBinding</title>
</head>
<body>




<label for="timezone">Subjects</label>

@Html.DropDownListFor(model => model.selectedid, new SelectList(Model.ddllist, "SubCategoryId", "SubCategoryName", Model.selectedid))



</body>
</html>


I am getting this error while executing this.
An expression tree may not contain a dynamic operation
I am new to ASP.Net MVC and please help me correct if something is wrong.Thanks

解决方案

This has been resolved now.

My dropdown razor code should be
@Html.DropDownListFor(model => model.selectedid, Model.ddllist)

since my ddllist property is already a List<selectlistitem>, it works fine with this code.


这篇关于如何绑定MVC5中的dropdownlist控件的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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