查看由构造函数链接到数据模型的模型

时间:2013-08-03 09:38:15

标签: asp.net-mvc asp.net-mvc-viewmodel

我没有任何其他方式来描述它然后我是如何在标题中做到的。

我有一个看起来像这样的视图模型:

public class CreateArtistModel
{        
    public IEnumerable<SelectListItem> Cultures { get; set; }
    public IEnumerable<SelectListItem> Products { get; set; }
    public Artists Artist { get; internal set; }

    /// <summary>
    /// Default constructor
    /// </summary>
    public CreateArtistModel() { }

    /// <summary>
    /// Non-default constructor with 1 parameter, culturelist
    /// </summary>
    /// <param name="cultures">List that can contain all the possible cultures.</param>
    /// <param name="products">List that can contain all possible products.</param>
    public CreateArtistModel(IEnumerable<SelectListItem> cultures, IEnumerable<SelectListItem> products) { Cultures = cultures; Products = products; }

    /// <summary>
    /// Non-default constructor that can have 1 parameter, artist
    /// </summary>
    /// <param name="artist">The artist with its various properties</param>
    public CreateArtistModel(Artists artist) { Artist = artist; }

    /// <summary>
    /// Non-default constructor that can have 2 parameter, a list cultureList and an atist
    /// </summary>
    /// <param name="cultures">List that can contain all the possible cultures.</param>
    /// <param name="products">List that can contain all possible products.</param>
    /// <param name="artist">The artist with its various properties</param>
    public CreateArtistModel(IEnumerable<SelectListItem> cultures, IEnumerable<SelectListItem> products, Artists artist)
    {
        Cultures = cultures;
        Products = products;
        Artist = artist;
    }

    [Required(ErrorMessage = "The artist's first name is required")]
    public string firstname { get; set; }

    [Required(ErrorMessage = "The artist's last name is required")]
    public string lastname { get; set; }

    public Nullable<int> dateOfBirth { get; set; }

    public Nullable<int> dateOfDeath { get; set; }

    [Required(ErrorMessage = "The artist's speciality is required")]
    public string speciality { get; set; }

    public string placeOfBirth { get; set; }
}

我的视图然后使用如下属性:

@model ArtWebShop.Areas.Admin.Models.CreateArtistModel

@Html.EditorFor(model => model.firstname)
@Html.ValidationMessageFor(model => model.firstname)

然后在我的控制器中我保存它:

        [HttpPost]
    public ActionResult Create(Artists artist)
    {
        if (!ModelState.IsValid)
        {
            return View(CreateArtistWithListOfProductsAndcultures(artist));
        }

        if (_unitOfWork != null)
        {
            _unitOfWork.ArtistsRepository.Create(artist);
            _unitOfWork.Save();
        }
        return RedirectToAction("Index", "Overview", new { area = "Admin" });
    }

这很有效! 但我不确定为什么......

我想明白为什么会这样。为什么我的视图模型中的属性会自动链接到艺术家模型?

1 个答案:

答案 0 :(得分:1)

我的模型粘合剂完成了魔术。默认的ASP.Net modelbinder具有从表单发布数据和\或查询字符串重新创建操作参数的功能。因此,当发生POST时,Modelbinder会启动并检查编码的后期数据,并尝试为您的操作方法构造对象。它根据参数 - 属性名称匹配进行映射。

这也意味着Modelbinder确实关心您的Model或ViewModel类。只要它在action参数中获得类类型,它就会尝试将POST(正文内容)或GET(查询字符串)参数映射到自定义类属性。你的ViewModel infact在这里是无关紧要的,除了它有助于渲染html表单,INPUT元素的名称与模型属性相匹配。

您可以查看这些文章,深入了解Modelbinders。

http://msdn.microsoft.com/en-us/magazine/hh781022.aspx

http://www.codeproject.com/Articles/159749/ASP-NET-MVC-Model-Binding-Part1

http://www.codeproject.com/Articles/161963/ASP-NET-MVC-Model-Binding-Part-2

相关问题