C# 如何在下拉列表的SelectList中设置所选项目?

C# 如何在下拉列表的SelectList中设置所选项目?,c#,asp.net,asp.net-mvc,entity-framework,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,我是MVC新手,但我已经通读了关于这个主题的几个问题。不过,没有一个答案能解决我的问题。以下是我的代码(只是相关属性): C#类 public class Customer { public int CustomerId { get; internal set; } public string Name { get; set; } } public class Project { public int ProjectId { get; internal set; }

我是MVC新手,但我已经通读了关于这个主题的几个问题。不过,没有一个答案能解决我的问题。以下是我的代码(只是相关属性):

C#类

public class Customer
{
    public int CustomerId { get; internal set; }
    public string Name { get; set; }
}

public class Project
{
    public int ProjectId { get; internal set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Comment { get; set; }

    public Customer CurrentCustomer { get; set; }
}
public ActionResult Edit(int id)
{
    Project item = projectRepository.Get(id);

    ViewBag.AllCustomers = new SelectList(
        new CustomerRepository().Get(), // Returns a List<Customer> of all active customers.
        "CustomerId",
        "Name",
        (object) item.CurrentCustomer.CustomerId);

    return View(item);
}
控制器类

public class Customer
{
    public int CustomerId { get; internal set; }
    public string Name { get; set; }
}

public class Project
{
    public int ProjectId { get; internal set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Comment { get; set; }

    public Customer CurrentCustomer { get; set; }
}
public ActionResult Edit(int id)
{
    Project item = projectRepository.Get(id);

    ViewBag.AllCustomers = new SelectList(
        new CustomerRepository().Get(), // Returns a List<Customer> of all active customers.
        "CustomerId",
        "Name",
        (object) item.CurrentCustomer.CustomerId);

    return View(item);
}
public ActionResult编辑(int-id)
{
Project item=projectRepository.Get(id);
ViewBag.AllCustomers=新建选择列表(
new CustomerRepository().Get(),//返回所有活动客户的列表。
“客户ID”,
“姓名”,
(对象)item.CurrentCustomer.CustomerId);
返回视图(项目);
}
查看

<div class="editor-label">
    @Html.LabelFor(model => model.CurrentCustomer)
</div>
<div class="editor-field">
    @Html.DropDownListFor(model => model.CurrentCustomer, (SelectList)ViewBag.AllCustomers, "-- Select a Customer. --")
    @Html.ValidationMessageFor(model => model.CurrentCustomer)
</div>
@Html.DropDownListFor(model => model.CurrentCustomerId,
 (SelectList)ViewBag.AllCustomers, "-- Select a Customer. --")

@LabelFor(model=>model.CurrentCustomer)
@Html.DropDownListFor(model=>model.CurrentCustomer,(SelectList)ViewBag.AllCustomers,“--选择一个客户。-”)
@Html.ValidationMessageFor(model=>model.CurrentCustomer)
我在没有使用ViewBag的情况下尝试了这个方法(在视图中执行SelectList实例化),但它也不起作用。我尝试过用编码一个ID来代替CurrentCustomer.CustomerId,但是当我在SelectList上设置一个断点时,我发现它可以正确地处理所有事情

所有其他StackOverflow问题都表明上述方法和属性名称应该可以正常工作,但就我个人而言,我无法找出这里出了什么问题。我遗漏了什么?

我怀疑您首先使用的是EF代码。您所犯的错误是试图将int(id)分配给实体
CurrentCustomer
dropdownlist
只返回一个id,而不是一个实体

您应该向操作方法发送一个Id,然后查找并分配实体(或者可以说只是外键)

在您的模型中添加Id

public class Project
{
    public int ProjectId { get; internal set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Comment { get; set; }

    public Customer CurrentCustomer { get; set; }
    public int CurrentCustomerId { get; set; }
}
在您看来

<div class="editor-label">
    @Html.LabelFor(model => model.CurrentCustomer)
</div>
<div class="editor-field">
    @Html.DropDownListFor(model => model.CurrentCustomer, (SelectList)ViewBag.AllCustomers, "-- Select a Customer. --")
    @Html.ValidationMessageFor(model => model.CurrentCustomer)
</div>
@Html.DropDownListFor(model => model.CurrentCustomerId,
 (SelectList)ViewBag.AllCustomers, "-- Select a Customer. --")

看起来你的<代码>铸造<代码>是向后的,是不是应该更像这样<代码>@Html.DropDownListFor(model=>model.currentcuster,ViewBag.AllCustomer作为SelectList…啊,我明白了!让我更新我的类和测试。我在这里查看示例时从未将这两个放在一起。是的--正是这样。太棒了!