2

所以我有一个非常简单的要求。我试图允许我的用户模型的编辑页面,但用户模型引用了一个基本上规定(管理员/常规/超级/等)的 UserTypeModel。我试图在编辑视图中填充一个组合框,以选择它们应该是什么用户类型。请参阅下面的代码:

public class UserModel : BaseModel
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Email { get; set; }

    public string Phone { get; set; }

    public UserTypeModel UserType { get; set; }

}

public class UserTypeModel
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }
}

这些是我的模型。这些是从数据库中提取的,因此在我的控制器中,我创建了所有可能的 UserTypeModel 的列表,并将它们与实际的 UserModel 一起发送到视图。请参阅下面的控制器代码:

public ActionResult Edit(int pId = 0)
{
    UserModel vUserModel = fMySqlService.GetUser(pId);

    List<UserTypeModel> vUserTypes = new List<UserTypeModel>(fMySqlService.GetAllUserTypes());
    SelectList vSelectList = new SelectList(vUserTypes, "Id", "Name", vUserModel.UserType);
    ViewBag.UserTypesList = vSelectList;

    return View(vUserModel);
}

正如您在上面看到的,我正在编辑特定的 UserModel(这也会使用所有属性 id/name/description 填充正确的 UserTypeModel)。然后我创建一个所有可能的 UserTypeModel 的列表,并通过 ViewBag 发送它。

现在是最后一块,编辑查看代码。我觉得这是我最坑的地方...

@model Project.Models.UserModel

@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Edit</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>UserModel</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FirstName)
            @Html.ValidationMessageFor(model => model.FirstName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LastName)
            @Html.ValidationMessageFor(model => model.LastName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Phone)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Phone)
            @Html.ValidationMessageFor(model => model.Phone)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.UserType.Name)
        </div>
        <div class="editor-field">
            @Html.DropDownList("UserType_Id", ViewBag.UserTypesList as SelectList)
        </div>

        @Html.HiddenFor(model => model.Id)

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

如您所见,我正在创建一个 ID/名称为“UserType_Id”的 DDL,并从 ViewBag.UserTypesList 中提取我的列表。理想情况下,我希望发生的是:

  1. SelectedValue 首先没有正确设置。我已经阅读了很多关于此的问题,但我仍然不确定如何解决我的问题。我希望正确地预先填充列表。
  2. 当用户更改 DDL 中的 SelectedValue 时,我希望我的模型返回到控制器并在 Model.UserType.Id 中设置正确的属性,但 Model.UserType 返回为空。

我不会费心发布编辑帖子的代码,只要知道参数是:

[HttpPost]
public ActionResult Edit(UserModel pUserModel)
4

0 回答 0