Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在MVC4的编辑模式下将值传递给下拉字段?_C#_Asp.net Mvc 4 - Fatal编程技术网

C# 如何在MVC4的编辑模式下将值传递给下拉字段?

C# 如何在MVC4的编辑模式下将值传递给下拉字段?,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,嗨,我的视图中有三个字段。这三个字段是下拉列表。我想在单击“编辑”按钮时将值传递给这些字段。这就是需要传递给该下拉字段的值。我的观点如下 在我看来,我有很多下拉列表,但一旦我知道如何将值传递给一个下拉列表,就意味着我将为另一个下拉列表执行操作 对于编辑,我在sql中创建了一个视图,并将该视图作为EDMX文件连接到我的应用程序中。在此视图(表)中,我有Visitors表单中的所有字段。该视图名为view\u VisitorsForm 我的模型(访问者查看模型) 我的视图代码 publ

嗨,我的视图中有三个字段。这三个字段是下拉列表。我想在单击“编辑”按钮时将值传递给这些字段。这就是需要传递给该下拉字段的值。我的观点如下

在我看来,我有很多下拉列表,但一旦我知道如何将值传递给一个下拉列表,就意味着我将为另一个下拉列表执行操作

对于编辑,我在sql中创建了一个视图,并将该视图作为EDMX文件连接到我的应用程序中。在此视图(表)中,我有Visitors表单中的所有字段。该视图名为view\u VisitorsForm

我的模型(访问者查看模型)

我的视图代码

      public ActionResult Edit(Guid ?id)
      {
             WafeERPNEWEntities db = new WafeERPNEWEntities();
        SelectList typelist = new SelectList(db.Employees.ToList(), "EmployeeID", "DisplayName",  db.Employees);
        ViewData["EmployeeName"] = typelist;
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        VisitorsViewModel ObjVisitorsviewModel = new VisitorsViewModel();
        View_VisitorsForm visit = db.View_VisitorsForm.Find(id);
        visit.VisitingID = ObjVisitorsviewModel.VisitingID;
        visit.VisitingDate = ObjVisitorsviewModel.Date;
        visit.Description = ObjVisitorsviewModel.Description;
                 if (ObjVisitorsviewModel == null)
        {
            return HttpNotFound();
        }

        return View(ObjVisitorsviewModel);
    }
        @Html.LabelFor(model => model.Date)
        @Html.EditorFor(model => model.Date)
        @Html.ValidationMessageFor(model => model.Date)

        @Html.LabelFor(model => model.VisitingID)
        @Html.EditorFor(model => model.VisitingID)
        @Html.ValidationMessageFor(model => model.VisitingID)

        @Html.LabelFor(model => model.EmployeeName)
        @Html.DropDownList("EmployeeID", (IEnumerable<SelectListItem>)ViewData["EmployeeName"])
        @Html.ValidationMessageFor(model => model.EmployeeName)
而且还可以访问visit.visitingID。但它没有在viewmodel中获取值。因此该值在视图中为空。所有值都为空VisitingID、Description、Date字段均为空,在Employee下拉列表中,它不会显示我传递给此字段的值,而是显示下拉列表中的第一个值。所以请任何人告诉我如何解决这个问题。事实上,我试图按照我的最佳水平解释我的问题,如果你不理解我的问题,或者任何人需要我的完整代码或需要更多代码,请告诉我。我准备再次更新我的代码。但我需要解决办法

提前感谢..

使用:

@Html.DropDownListFor(model => model.EmployeeID,(IEnumerable<SelectListItem>)ViewData["EmployeeName"])
@Html.DropDownListFor(model=>model.EmployeeID,(IEnumerable)ViewData[“EmployeeName”])
重要的部分是“DropDownListFor”。您正在使用“DropDownList”。

使用:

@Html.DropDownListFor(model => model.EmployeeID,(IEnumerable<SelectListItem>)ViewData["EmployeeName"])
@Html.DropDownListFor(model=>model.EmployeeID,(IEnumerable)ViewData[“EmployeeName”])

重要的部分是“DropDownListFor”。您使用的是“DropDownList”。

您使用的是
DropDownList(…)
而不是
DropDownList for(…)

您的型号

    public Nullable<System.DateTime> Date { get; set; }
    public System.Guid VisitingID { get; set; }
    public Nullable<System.Guid> EmployeeID { get; set; }
    public string EmployeeName { get; set; }
    public string Description { get; set; }
您必须添加选择列表:

public SelectList Employees { get; set }
您的编辑

您必须获取员工列表并将其添加到模型中:

// Get employees list from the database
var employees = db.Employee.Select(x => x.Id, x.Name).Tolist();

// Put the employees in a SelectList
var selectList = new SelectList(employees .Select(x => new { value = x.Id, text = x.Name }), "value", "text");}).ToList();

// Pass the list to your ViewModel
ObjVisitorsviewModel.Employees = selectList;
你的观点

最后,将DropDownListFor行更改为:

@Html.DropDownListFor(model => model.EmployeeID,
                               model.Employees)

通过使用
下拉列表(…)
,对象数据不会绑定到下拉列表。您必须手动管理其所选值。

您使用的是
DropDownList(…)
而不是
DropDownListFor(…)

您的型号

    public Nullable<System.DateTime> Date { get; set; }
    public System.Guid VisitingID { get; set; }
    public Nullable<System.Guid> EmployeeID { get; set; }
    public string EmployeeName { get; set; }
    public string Description { get; set; }
您必须添加选择列表:

public SelectList Employees { get; set }
您的编辑

您必须获取员工列表并将其添加到模型中:

// Get employees list from the database
var employees = db.Employee.Select(x => x.Id, x.Name).Tolist();

// Put the employees in a SelectList
var selectList = new SelectList(employees .Select(x => new { value = x.Id, text = x.Name }), "value", "text");}).ToList();

// Pass the list to your ViewModel
ObjVisitorsviewModel.Employees = selectList;
你的观点

最后,将DropDownListFor行更改为:

@Html.DropDownListFor(model => model.EmployeeID,
                               model.Employees)

通过使用
下拉列表(…)
,对象数据不会绑定到下拉列表。您必须手动管理其所选值。

使用
DropDownListFor
helper方法

 @Html.DropDownListFor(model => model.EmployeeID,
                                    (IEnumerable<SelectListItem>)ViewData["EmployeeName"])
一个更干净的解决方案是不使用ViewData传输渲染下拉选项所需的数据。只需在视图模型中添加一个新属性,就可以使代码具有更强的类型

public class VisitorsViewModel
{
   public List<SelectListItem> Employees { set;get;}
   public Guid? EmployeeID { get; set; }
   // Your existing properties goes here
}
在您看来,我们将在Employees属性中使用DropDownList for helper方法

@model VisitorsViewModel
@using(Html.BeginForm())
{
  @Html.DropDownListFor(s=>s.EmployeeID,Model.Employees,"Select")  
}

使用
DropDownListFor
helper方法

 @Html.DropDownListFor(model => model.EmployeeID,
                                    (IEnumerable<SelectListItem>)ViewData["EmployeeName"])
一个更干净的解决方案是不使用ViewData传输渲染下拉选项所需的数据。只需在视图模型中添加一个新属性,就可以使代码具有更强的类型

public class VisitorsViewModel
{
   public List<SelectListItem> Employees { set;get;}
   public Guid? EmployeeID { get; set; }
   // Your existing properties goes here
}
在您看来,我们将在Employees属性中使用DropDownList for helper方法

@model VisitorsViewModel
@using(Html.BeginForm())
{
  @Html.DropDownListFor(s=>s.EmployeeID,Model.Employees,"Select")  
}

Maxime您这样说var employeesList=GetEmployees()。这意味着var employeeList=db.Employee.Tolist();像这样??Maxime你说的var employeesList=GetEmployees()像这样。这意味着var employeeList=db.Employee.Tolist();像这样?这是正确的方法。如果仍然没有选择正确的员工,那么您应该确保模型的EmployeeID存在于选择列表的员工列表中,并且两者都具有相同的EmployeeID。这是正确的方法。如果仍然没有选择正确的员工,那么您应该确保您的模型的EmployeeID存在于您的选择列表的员工列表中,并且两者都具有相同的EmployeeID。在employee下拉列表中,它不会显示我传递给此字段的值?您尚未向模型传递任何值!(没有,您曾在控制器中的哪里设置过
EmployeeID
的值)您是否能够使事情正常运行?在“员工”下拉列表中,它不会显示我传递给此字段的值?您尚未向模型传递任何值!(没有,你曾在你的控制器中设置过
EmployeeID
的值)你能使事情正常工作吗?Shyju我在vm.Employees=db.Employees.Select(s=>new SelectListItem{value=s.employeed.ToString(),Text=s.DisplayName})一行中发现了这个错误;下面提到了错误。System.NotSupportedException:LINQ to Entities无法识别“System.String ToString()”方法,并且无法将此方法转换为存储表达式。EmployeId列的类型是什么?如果它是Guid,代码应该可以正常工作。我只是亲自核实了一下。您可以对LINQ表达式中的Guid属性调用
ToString()
。Shyju我在vm.Employees=db.Employees.Select(s=>new-SelectListItem{Value=s.EmployeId.ToString(),Text=s.DisplayName})行中遇到此错误;下面提到了错误。System.NotSupportedException:LINQ to Entities无法识别“System.String ToString()”方法,并且无法将此方法转换为存储表达式。EmployeId列的类型是什么?如果它是Guid,代码应该可以正常工作。我只是亲自核实了一下。可以对LINQ表达式中的Guid属性调用
ToString()