模型绑定不工作 [英] Model Binding not working

查看:117
本文介绍了模型绑定不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下POCO类:

public class Location
    {
        public int LocationId { get; set; }
        public string Name { get; set; }
        public string Street { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
        public string Country { get; set; }
        public float? Latitude { get; set; }
        public float? Longitude { get; set; }
        public string PhoneNumber { get; set; }
        public string EmailAddress { get; set; }
        public string Website { get; set; }
        public virtual ICollection<Program> Programs { get; set; }
        public virtual ICollection<PlayerType> PlayerTypes { get; set; }
    }

public class PlayerType
    {
        public int PlayerTypeId { get; set; }
        public string Name { get; set; }
        public int SortOrder { get; set; }
        public bool IsActive { get; set; }
        public virtual ICollection<Location> Locations { get; set; }
    }

和视图模型类

public class LocationViewModel
    {
        public Location Location { get; set; }
        public IList<PlayerType> SelectPlayerTypes { get; set; } 

        public LocationViewModel()
        {
            Location = new Location();
        }

    }

在我的创建表单,我已经定义了模型

Within my Create Form, I have defined the model as

@model Locator.Models.LocationViewModel

和有像以下字段:

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

在我的控制器来处理POST我有

In my controller to handle the POST I have

[HttpPost]
        public ActionResult Create(LocationViewModel location)
        {
            if (ModelState.IsValid) {
                locationRepository.InsertOrUpdate(location.Location);
                locationRepository.Save();
                return RedirectToAction("Index");
            }
            location.SelectPlayerTypes = golferTypeRepository.All.Where(p => p.IsActive).ToList();
            return View(location);
        }

问题是,我有一个定位对象,但没有任何属性都设置为在表单中输入的值。

The problem is that I have a Location object but none of the properties are to set to the values entered in the form.

难道我做错了吗?

感谢

推荐答案

下面的问题:

[HttpPost]
public ActionResult Create(LocationViewModel location)

你看到了吗?这是你的操作参数的名称:位置

看看你的观点,现在的模式,它有一个名为位置属性:

Look at your view model now, it has a property named Location:

public Location Location { get; set; }

这混淆模型绑定。它不再知道你是否需要将 LocationViewModel 或它的属性绑定。

This confuses the model binder. It no longer knows whether you need to bind the LocationViewModel or its property.

所以,简单地重命名为避免冲突:

So simply rename to avoid the conflict:

[HttpPost]
public ActionResult Create(LocationViewModel model)

这篇关于模型绑定不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆