使用 LINQ/MVC3/C# 绑定下拉列表时出现问题

发布于 2024-11-24 11:17:54 字数 2290 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

又怨 2024-12-01 11:19:17

如果您使用模型类,请尝试以下操作:

<%: Html.DropDownList("EmpId", new SelectList(Model, "EmpId", "EmpName")) %>

如果您想为下拉列表添加点击事件,请尝试以下操作:

<%: Html.DropDownList("courses", ViewData["courses"] as SelectList, new { onchange = "redirect(this.value)" }) %>

在上面的 redirect(this.value) 中,是一个 JavaScript 函数

If you are using a model class just try this:

<%: Html.DropDownList("EmpId", new SelectList(Model, "EmpId", "EmpName")) %>

If you want to add click event for drop down list try this:

<%: Html.DropDownList("courses", ViewData["courses"] as SelectList, new { onchange = "redirect(this.value)" }) %>

In the above one redirect(this.value) is a JavaScript function

究竟谁懂我的在乎 2024-12-01 11:19:03

试试这个:

public class TimeProfileController : Controller
{
    private static String strConnString = ConfigurationManager.ConnectionStrings["timekeepingConnectionString"].ConnectionString;

    [HttpGet]
    public ActionResult ProfileSelect()
    {
        var dataContext = new profileConnectionDataContext(strConnString);
        var profiles = dataContext.TimeProfiles.ToArray().Select(x => new SelectListItem
        {
            Value = x.Profile_ID,
            Text = x.profilename
        });
        var model = new ProfileViewModel
        {
            profiles = profiles
        };
        return View(model);
    }
}

并在视图中:

@model ProfileViewModel
@Html.DropDownListFor(
    x => x.Profile_ID,
    new SelectList(Model.profiles, "Values", "Text"),
    "-- Select--"
)

Try this:

public class TimeProfileController : Controller
{
    private static String strConnString = ConfigurationManager.ConnectionStrings["timekeepingConnectionString"].ConnectionString;

    [HttpGet]
    public ActionResult ProfileSelect()
    {
        var dataContext = new profileConnectionDataContext(strConnString);
        var profiles = dataContext.TimeProfiles.ToArray().Select(x => new SelectListItem
        {
            Value = x.Profile_ID,
            Text = x.profilename
        });
        var model = new ProfileViewModel
        {
            profiles = profiles
        };
        return View(model);
    }
}

and in the view:

@model ProfileViewModel
@Html.DropDownListFor(
    x => x.Profile_ID,
    new SelectList(Model.profiles, "Values", "Text"),
    "-- Select--"
)
追我者格杀勿论 2024-12-01 11:18:50

希望它一定能工作...

内部控制器:

var lt = from result in db.Employees select new { result.EmpId, result.EmpName };
ViewData[ "courses" ] = new SelectList( lt, "EmpId", "EmpName" );

内部视图:

<%: Html.DropDownList("courses") %>

Hope it will work surely...

Inside Controller :

var lt = from result in db.Employees select new { result.EmpId, result.EmpName };
ViewData[ "courses" ] = new SelectList( lt, "EmpId", "EmpName" );

Inside View :

<%: Html.DropDownList("courses") %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文