ASP.NET MVC 模型绑定器不会绑定我的视图模型

本文关键字:模型 绑定 我的 视图 NET ASP MVC | 更新日期: 2023-09-27 18:34:32

这是我的视图模型,我绑定到视图。

public class EmployeeMasterViewModel
{
    public EmployeeViewModel Employee { get; set; }
    public List<SkillViewModel> Skills { get; set; }
}

已成功绑定Employee属性,并且所有属性都已填充
但这就是问题所在,Skills属性,即SkillViewModel列表,

public class SkillViewModel
{
    public int Id { get; set; }
    public string SkillName { get; set; }
}

提交表单时Skills.Count为 0

   @for (int i = 0; i < Model.Skills.Count; i++)
{
        SkillViewModel skill = Model.Skills[i];
        string id = skill.Id + "-" + String.Join("-", skill.SkillName.Split(new[] { " " },             StringSplitOptions.RemoveEmptyEntries));
        <tr id="@id">
            <td>
                @Html.HiddenFor(e => e.Skills[i].Id)
                @Html.HiddenFor(e => e.Skills[i].SkillName)
                @skill.SkillName
            </td>
            <td>
                <a href="javascript:deleteSkill('@id')">
                    <span class="label label-sm label-danger">
                        Delete
                    </span>
                </a>
            </td>
        </tr>
    }

控制器

    public ActionResult Create()
    {
        ViewBag.CanCreateEmployee = Right.CanCreateEmployee;
        ViewBag.Skills = Common.GetSkills();
        var viewModel = new EmployeeMasterViewModel();
        var employee = new EmployeeViewModel
        {
            Departments = Common.GetDepartments(),
            Designations = Common.GetDesignations(),
            Grades = Common.GetGrades(),
            MaritalStatuses = Common.GetMaritalStatuses(),
            Genders = Common.GetGenders(),
        };
        viewModel.Skills = new List<SkillViewModel>();
        viewModel.Skills.Add(new SkillViewModel
        {
            Id = 1,
            SkillName = "Test Driven Developement"
        });
        viewModel.Employee = employee;
        return View(viewModel);
    }

开机自检操作

    [HttpPost]
    public ActionResult Create(EmployeeMasterViewModel viewModel, HttpPostedFileBase file)
    {
        if (!Right.CanCreateEmployee)
        {
            Session["ErrorNotify"] = "You don't have permission to add new employee";
            return RedirectToAction("Index");
        }

        viewModel.Employee.Departments = Common.GetDepartments();
        viewModel.Employee.Designations = Common.GetDesignations();
        viewModel.Employee.Grades = Common.GetGrades();
        viewModel.Employee.MaritalStatuses = Common.GetMaritalStatuses();
        viewModel.Employee.Genders = Common.GetGenders();
        viewModel.Skills = _model.GetEmployeeSkills();
        ViewBag.Skills = Common.GetSkills();
        ViewBag.CanCreateEmployee = Right.CanCreateEmployee;
        if (!CheckErrors(viewModel))
        {
            return View(viewModel);
        }
        string image = Common.ImageUpload(file);
        if (image == "0")
        {
            ModelState.AddModelError("ImageTypeError", "Image type not supported");
            return View(viewModel);
        }
        viewModel.Employee.ImgPath = image;
        _model.Insert(viewModel.Employee);
        Session["SuccessNotify"] = "Employee is successfully registered";
        return RedirectToAction("Index");
    }

ASP.NET MVC 模型绑定器不会绑定我的视图模型

我试图复制您的问题,但我的尝试工作正常。

查看代码

@model SO.Controllers.EmployeeMasterViewModel
@using (Html.BeginForm())
{
    @Html.TextBoxFor(m => m.Employee.EmployeeName);
    <input data-val="true" name="Skills[0].Id" type="hidden" value="1">
    <input name="Skills[0].SkillName" type="hidden" value="Test Driven Developement">
    <input type="submit" value="Save" />
}

控制器代码

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var vm = new EmployeeMasterViewModel();
        vm.Employee = new EmployeeViewModel();
        vm.Skills = new List<SkillViewModel>();
        return View(vm);
    }
    [HttpPost]
    public ActionResult Index(EmployeeMasterViewModel viewModel)
    {
        return View(viewModel);
    }
}

当我提交表单时,所有数据都正确传递,包括技能数据。也许你可以用这个答案作为参考来帮助你?