下拉列表的选择值重置表单提交

时间:2015-04-16 07:25:52

标签: asp.net-mvc entity-framework

当我通过更改下拉选择值来编辑成员成员类型时,在回发时,所选值会将自身重置为顶部项目,并且用户将获得该成员类型。

我出错的任何想法?

查看 -

@Html.DropDownListFor(model => model.SelectedMemberType, new SelectList(Model.MemberTypes, "ID", "Name", (int)Model.MemberType))

模型 -

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

    public int CustomerId { get; set; }

    [Required]
    [DisplayName("First Name")]
    public string FirstName { get; set; }

    [Required]
    [DisplayName("Last Name")]
    public string LastName { get; set; }

    public int SelectedMemberType { get; set; }

    [Required]
    [DisplayName("Member Type")]
    public int MemberType { get; set; }

    public virtual IList<MemberTypeModel> MemberTypes { get; set; }

    public MemberDetailModel()
    {
        MemberTypes = new List<MemberTypeModel>();
    }
}

控制器 -

[Authorize]
    public ActionResult Details(int id = 0)
    {
        ViewBag.MemberType = new SelectList(_memberTypeService.GetAll(), "ID", "Name");

        MemberDetailModel member = _memberService.GetById(id);

        IEnumerable<Gender> genders = Enum.GetValues(typeof(Gender))
                                                   .Cast<Gender>();

        return View(member);
    }

    [HttpPost]
    public ActionResult Details(MemberDetailModel model)
    {
        model.MemberType = model.SelectedMemberType;

        if (ModelState.IsValid)
        {
            _memberService.UpdateMember(model);

            return View(member);
        }

        return View(model);
    }

1 个答案:

答案 0 :(得分:0)

由于选择列表将在回发之间丢失,因此不要忘记在回发时再次设置MemberTypes。大多数情况都是问题。