简体   繁体   English

将SelectedItem从下拉列表绑定到MVC 4中的模型对象

[英]Binding SelectedItem from dropdownlist to model object in MVC 4

I'm very new with MVC, so bear with me, but I can't seem to bind a value from a SelectList to an instance of the selected object during a postback in MVC 4. 我对MVC非常陌生,所以请耐心等待,但是在MVC 4中进行回发时,我似乎无法将SelectList的值绑定到所选对象的实例。

Suppose I have to create a Teacher as a member of a School. 假设我必须创建一名教师作为学校成员。 I have a ViewModel class defined as such: 我有这样定义的ViewModel类:

public class RegisterTeacherModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    public string Email { get; set; }

    [Required]
    [Display(Name = "School")]
    public School SelectedSchool { get; set; }

    [ScaffoldColumn(false)]
    public Guid UserId
    {
        get;
        set;
    }

    public SelectList PossibleSchools
    {
        get;
        private set;
    }

    public RegisterTeacherModel(IRepository<School> schoolRepo)
    {
        PossibleSchools = new SelectList(schoolRepo, "Id", "Name");
    }
}

And my View: 我的看法:

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

<fieldset>
    <legend>RegisterTeacherModel</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.UserName)
    </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.SelectedSchool)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.SelectedSchool, Model.PossibleSchools)
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

And finally, my Controller method: 最后,我的Controller方法:

    [HttpPost, ActionName("Create")]
    public ActionResult CreateTeacher(RegisterTeacherModel teacherModel)
    {
        if (ModelState.IsValid)
        {
            try
            {
              ...
            }
         }
    }

But when I receive the RegisterTeacherModel object back in my Create method in the Controller, SelectedSchool is always null. 但是,当我在Controller的Create方法中重新收到RegisterTeacherModel对象时,SelectedSchool始终为null。 I must be missing something in the way the model binder re-creates the object references on postback. 我必须在模型活页夹在回发时重新创建对象引用的方式中缺少某些东西。 Can anyone point me in the right direction? 谁能指出我正确的方向?

I think you touched on it with your second post: Try pointing the initial code to Model.SelectedSchool. 我想您在第二篇文章中谈到了它:尝试将初始代码指向Model.SelectedSchool。 <IdProperty>

<div class="editor-field">
  @Html.DropDownListFor(model => model.SelectedSchool.**<IdProperty>**, Model.PossibleSchools)
</div>

Well, I found a workaround. 好吧,我找到了解决方法。 I still don't know if I'm missing something, but instead of using a School object in my ViewModel, I replaced it with the SelectedSchoolId as such: 我仍然不知道我是否缺少什么,但是我没有在ViewModel中使用School对象,而是将其替换为SelectedSchoolId:

public class RegisterTeacherModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    public string Email { get; set; }

    [Required]
    [Display(Name = "School")]
    public int SelectedSchoolId { get; set; }

    ...
}

And change my View dropdown to use this instead: 并更改我的“视图”下拉列表以改为使用它:

<div class="editor-field">
    @Html.DropDownListFor(model => model.SelectedSchoolId, Model.PossibleSchools)
</div>

And then in my controller, when creating the real model objects I can simply pull the School object from the School repository and associate it with the real Teacher object. 然后在我的控制器中,当创建真实的模型对象时,我可以简单地从School仓库中提取School对象并将其与真实的Teacher对象关联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM