DropDownList的MVC中? [英] DropDownList in MVC?
本文介绍了DropDownList的MVC中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的appliaction一个DropDownList我已经通过数据库中的记录绑定
但在任何修改表单我想设置的下拉列表中选定的指标我怎能做到这一点,我在这里Pastng我的code。
code绑定DropDownList的
公共IList的GetFeedbackList()
{
INT feedbackId = 0;
字符串feedbackName =的String.Empty; 使用(VAR DB =新brandconnectionsEntities())
{
在db.BC_FeedbackBy返回(IList的)(从s
新选择
{ feedbackId = s.FeedbackById,
feedbackName = s.FeedbackBy,
})
.ToList();
}
}// code,返回列表IList的allfeedbacks = _dropdownProvider.GetFeedbackList();计算机[feedback_for] =新的SelectList(allfeedbacks,feedbackId,feedbackName);//在视图页面
<%= Html.DropDownList(feedback_for计算机[feedback_for]作为的SelectList - 选择 - 新{@class =inputtext1})%GT;
请告诉应当如何设定我从数据库中选定的指数
谢谢
丽思
解决方案
计算机[feedback_for] =新的SelectList(allfeedbacks,feedbackId,feedbackName的SelectedValue);
至少在MVC2。
我的时候我给下拉列表中名称相同的密钥这消失的问题。即:
<%= Html.DropDownList(feedbackId计算机[feedback_for]作为的SelectList - 选择 - 新{@class =inputtext1} )%GT;
I am having a dropdownlist in my appliaction which I have binded through the database records but on any of the edit form I want to set the selected index of the dropdown list how shall I do that ,I am Pastng my code here.
Code for binding the dropdownlist
public IList GetFeedbackList()
{
int feedbackId = 0;
string feedbackName = string.Empty;
using (var db = new brandconnectionsEntities())
{
return (IList )(from s in db.BC_FeedbackBy
select new
{
feedbackId =s.FeedbackById ,
feedbackName=s.FeedbackBy ,
})
.ToList ();
}
}
//Code for returning the list
IList allfeedbacks = _dropdownProvider.GetFeedbackList();
ViewData["feedback_for"] = new SelectList(allfeedbacks, "feedbackId", "feedbackName");
//In the View Page
<%=Html.DropDownList("feedback_for", ViewData["feedback_for"] as SelectList, "--Select--", new { @class = "inputtext1" })%>
Please tell how shall I set the selected index from database
Thanks Ritz
解决方案
ViewData["feedback_for"] = new SelectList(allfeedbacks, "feedbackId", "feedbackName", selectedvalue);
At least in MVC2.
I had a problem which disappeared when I gave the dropdown the same name as the key. Ie:
<%=Html.DropDownList("feedbackId", ViewData["feedback_for"] as SelectList, "--Select--", new { @class = "inputtext1" })%>
这篇关于DropDownList的MVC中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文