使用 LINQ/MVC3/C# 绑定下拉列表时出现问题
我对 MVC3 和 C# 非常陌生,所以请原谅我的菜鸟问题。我已经为此苦苦挣扎了几乎一整天,希望有人能提供一些线索(我也在这个网站上寻找线索、提示、答案)。
我的数据库中有一个表,它将保存所有数据。它用于配置文件编辑器,它将存储用户可以在选择时自动填充其计时条目表单的值。不过,我正在第一步,尝试使用配置文件名称填充第一个下拉列表。我正在使用 LINQ、MVC3/Razer 和 C#。
这是我的 dbml: 无法发布图片,因为我是新人
http://imageshack.us/photo/my -images/560/timem.png/
这是我的模型:
namespace Timekeeping.Models
{
public class Profile
{
public int Profile_ID { get; set; }
public string profilename { get; set; }
public string networkuserid { get; set; }
public int projectnumber { get; set; }
public int costcode { get; set; }
public int paycode { get; set; }
public int jobtype { get; set; }
public int workorder { get; set; }
public int activity { get; set; }
public string taxarea { get; set; }
public IEnumerable<Profile> profiles { get; set; }
}
public class ProfileViewModel
{
public int Profile_ID { get; set; }
public IEnumerable<SelectListItem> profiles { get; set; }
}
}
这是我的控制器:
namespace Timekeeping.Controllers
public class TimeProfileController : Controller
{
private static String strConnString = ConfigurationManager.ConnectionStrings["timekeepingConnectionString"].ConnectionString;
[HttpGet]
public ActionResult ProfileSelect()
{
profileConnectionDataContext dataContext = new profileConnectionDataContext(strConnString);
var model = new ProfileViewModel();
var rsProfile = from fbs in dataContext.TimeProfiles select fbs;
ViewData["ProfileList"] = new SelectList(rsProfile, "Profile_ID", "profilename");
return View(model);
}
}
这是我为我的视图尝试过的所有不同的 html 帮助器(没有工作):
@Html.DropDownList("rsProfile", (SelectList)ViewData["ProfileList"]))
@Html.DropDownListFor(
x => x.Profile_ID,
new SelectList(Model.profiles, "Values", "Text"),
"-- Select--"
)
@Html.DropDownListFor(model => model.Profile_ID, Model.profilename)
我知道这看起来很混乱在,但我是希望有人能帮助我,这样我就能继续困难的部分。预先感谢我从社区获得的任何帮助
I am super new to MVC3 and C#, so excuse my noob questions. I have been struggling with this for almost a full day and hope someone can shed some light (I have scoured this site for clues, hints, answers as well).
I have a single table in my database which will hold all the data. It is for a profile editor which will store values that the user can populate their timekeeping entry form automatically upon select. I am on step one though, trying to populate the first dropdownlist with the profile name. I am using LINQ, MVC3/ Razer, and C#.
Here is my dbml:
Cant post image cause I am new
http://imageshack.us/photo/my-images/560/timem.png/
Here is my model:
namespace Timekeeping.Models
{
public class Profile
{
public int Profile_ID { get; set; }
public string profilename { get; set; }
public string networkuserid { get; set; }
public int projectnumber { get; set; }
public int costcode { get; set; }
public int paycode { get; set; }
public int jobtype { get; set; }
public int workorder { get; set; }
public int activity { get; set; }
public string taxarea { get; set; }
public IEnumerable<Profile> profiles { get; set; }
}
public class ProfileViewModel
{
public int Profile_ID { get; set; }
public IEnumerable<SelectListItem> profiles { get; set; }
}
}
Here is my Controller:
namespace Timekeeping.Controllers
public class TimeProfileController : Controller
{
private static String strConnString = ConfigurationManager.ConnectionStrings["timekeepingConnectionString"].ConnectionString;
[HttpGet]
public ActionResult ProfileSelect()
{
profileConnectionDataContext dataContext = new profileConnectionDataContext(strConnString);
var model = new ProfileViewModel();
var rsProfile = from fbs in dataContext.TimeProfiles select fbs;
ViewData["ProfileList"] = new SelectList(rsProfile, "Profile_ID", "profilename");
return View(model);
}
}
And here are all the different html helpers I have tried for my View(none work):
@Html.DropDownList("rsProfile", (SelectList)ViewData["ProfileList"]))
@Html.DropDownListFor(
x => x.Profile_ID,
new SelectList(Model.profiles, "Values", "Text"),
"-- Select--"
)
@Html.DropDownListFor(model => model.Profile_ID, Model.profilename)
I know this is a mess to look at, but I am hoping someone can help so I can get on with the hard parts. Thanks in advance for any help I get from the community
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用模型类,请尝试以下操作:
如果您想为下拉列表添加点击事件,请尝试以下操作:
在上面的
redirect(this.value)
中,是一个 JavaScript 函数If you are using a model class just try this:
If you want to add click event for drop down list try this:
In the above one
redirect(this.value)
is a JavaScript function试试这个:
并在视图中:
Try this:
and in the view:
希望它一定能工作...
内部控制器:
内部视图:
Hope it will work surely...
Inside Controller :
Inside View :