使用实体框架从 ASP.NET MVC (C#) 中的 DropDown 获取 SelectedValue

发布于 2024-08-14 12:47:21 字数 2020 浏览 5 评论 0原文

抱歉,如果这是一个重复的问题,我扫描了相关问题,但没有看到任何明显的内容。

我使用带有实体对象的 EditModel 以及其中的两个 SelectList。问题是,一旦我到达 POST 操作,两个下拉列表的 SelectedValues 仍然是我在模型构造函数中设置的相同默认值,无论我在浏览器上实际选择什么。

我的构造函数为 SelectedValues 设置了一些默认值,但它们只是 0 和“”(在下拉列表中不是有效值)。我感觉问题以某种方式围绕着这个问题,但我会提供更多细节。

这是模型的精简版本:

public class UserAccountModel 
{

    public UserAccount UserAccountData { get; set; } // Entity from EF
    public SelectList Organizations { get; set; }
    public SelectList Roles { get; set; }

    public UserAccountModel() : this(null)
    {
    }


    public UserAccountModel(UserAccount obj)
    {
        UserAccountData = (obj == null) ? new UserAccount() : obj;

        // default selected value
        int orgChildId = (UserAccountData.Organization == null) ? 0 : UserAccountData.Organization.ID;
        string roleChildId = (UserAccountData.Role == null) ? "" : UserAccountData.Role.Name;

        // go get the drop down options and set up the property binding
        using (UserAccountRepository rep = new UserAccountRepository())
        {
            Organizations = new SelectList(rep.GetOrganizationOptions(), "ID", "ID", orgChildId);
            Roles = new SelectList(rep.GetRoleOptions(), "ID", "Name", roleChildId);
        }
    }

    public void UpdateModel()
    {
        UserAccountData.Organization = Organizations.SelectedValue as Organization;
        UserAccountData.Role = Roles.SelectedValue as Role;
    }
}

这是视图的下拉菜单部分:

                <div class="field">
                <label for="ID">Organization:</label>
                <%= Html.DropDownList("ID", Model.Organizations) %>
            </div>

            <div class="field">
                <label for="Name">Role:</label>
                <%= Html.DropDownList("Name", Model.Roles) %>
            </div>

我可能在这里做了一些愚蠢而明显的事情。使用 ViewData 字典时,这些示例更加直接,但我找不到太多尝试对 SelectList 使用直接模型绑定的示例。

非常感谢任何帮助!

克里斯

Sorry if this is a repeated question, I scanned the related questions and didn't see anything obvious.

I'm using an EditModel with an Entity object, along with two SelectLists in it. The problem is, once I reach my POST action, the SelectedValues for both drop downs are still the same default values I set in the constructor for the model, no matter what I actually select on the browser.

My constructor sets some default values for the SelectedValues, but they are just 0 and "" (which aren't valid values in the dropdowns). I have a feeling the problem revolves around that somehow, but I'll give more details.

Here is a stripped down version of the model:

public class UserAccountModel 
{

    public UserAccount UserAccountData { get; set; } // Entity from EF
    public SelectList Organizations { get; set; }
    public SelectList Roles { get; set; }

    public UserAccountModel() : this(null)
    {
    }


    public UserAccountModel(UserAccount obj)
    {
        UserAccountData = (obj == null) ? new UserAccount() : obj;

        // default selected value
        int orgChildId = (UserAccountData.Organization == null) ? 0 : UserAccountData.Organization.ID;
        string roleChildId = (UserAccountData.Role == null) ? "" : UserAccountData.Role.Name;

        // go get the drop down options and set up the property binding
        using (UserAccountRepository rep = new UserAccountRepository())
        {
            Organizations = new SelectList(rep.GetOrganizationOptions(), "ID", "ID", orgChildId);
            Roles = new SelectList(rep.GetRoleOptions(), "ID", "Name", roleChildId);
        }
    }

    public void UpdateModel()
    {
        UserAccountData.Organization = Organizations.SelectedValue as Organization;
        UserAccountData.Role = Roles.SelectedValue as Role;
    }
}

This is the Dropdowns portion of the view:

                <div class="field">
                <label for="ID">Organization:</label>
                <%= Html.DropDownList("ID", Model.Organizations) %>
            </div>

            <div class="field">
                <label for="Name">Role:</label>
                <%= Html.DropDownList("Name", Model.Roles) %>
            </div>

I might have done something stupid and obvious here. The examples are much more straight forward when using the ViewData dictionary, but I couldn't find too many examples trying to use straight model binding for SelectLists.

Any help is greatly appreciated!

Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

多像笑话 2024-08-21 12:47:21

选择元素仅回发所选元素的实际值。在这种情况下,服务器/控制器将接收回参数作为 ID(组织)和名称(角色)。用于更新操作的模型应该包含这些作为属性,或者您的控制器操作应该直接接受它们作为参数。模型上的列表不会被重新填充——并且名称在任何情况下都不匹配。

通过添加以下内容来修改模型:

public int ID { get; set; }
public string Name { get; set; }

使用控制器操作为:

public ActionResult Update( UserAccountModel userAccount )
{
    ...
}

请注意,如果存在验证错误,则需要重新填充 SelectList 属性(重新构建菜单)。

Select elements only post back the actual value of the selected it. In this case, the parameters will be received back at the server/controller as ID (organization) and Name (role). The model you use for the update action should either contain these as properties or your controller action should accept them directly as parameters. The lists on the model won't be repopulated -- and the names don't match in any event.

Modify your model by adding:

public int ID { get; set; }
public string Name { get; set; }

with controller action as:

public ActionResult Update( UserAccountModel userAccount )
{
    ...
}

Note that if there is a validation error, you'll need to repopulate the SelectList properties (reconstituting the menus).

迟到的我 2024-08-21 12:47:21

您只是忘记了 ID 属性的导航路径:)

<%= Html.DropDownList("Organization.ID", Model.Organizations) %>

You simply forgot the navigational path to the ID property :)

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