asp.net-mvc-4 - 完成未绑定(bind)的 MVC 模型属性的最佳实践?

标签 asp.net-mvc-4 model-binding

我正在尝试寻找对仅部分编辑的模型使用 MVC 的最佳方法。

下面是一个简单的例子。

型号

using System.ComponentModel.DataAnnotations;
public class SimpleModel
{
    public int Id { get; set; }
    public string Parent { get; set; }
    [Required]
    public string Name { get; set; }
}

查看

using System.ComponentModel.DataAnnotations;
public class SimpleModel
{
    public int Id { get; set; }
    public string Parent { get; set; }
    [Required]
    public string Name { get; set; }
}

Controller

using System.Web.Mvc;

public class SimpleController : Controller
{
    public ActionResult Edit(int id)
    { return View(Get(id)); }

    [HttpPost]
    public ActionResult Edit(int id, SimpleModel model)
    {
        if (model.Name.StartsWith("Child")) //Some test that is not done client-side.
        {
            Save(model);
            //Get the saved data freshly.
            //model = Get(id);
        }
        else
        {
            ModelState.AddModelError("", "Name should start with 'Child'");
        }
        //Is this the way to set the Parent property?
        //var savedModel = Get(id);
        //model.Parent = savedModel.Parent;
        return View(model);
    }

    //Mock a database.
    SimpleModel savedModel;

    private void Save(SimpleModel model)
    { savedModel = new SimpleModel() { Id = model.Id, Name = model.Name }; }

    private SimpleModel Get(int id)
    {
        if (savedModel == null)
        { return new SimpleModel() { Id = id, Parent = "Father", Name = "Child " + id.ToString() }; }
        else
        { return new SimpleModel() { Id = savedModel.Id, Parent = "Father", Name = savedModel.Name }; }
    }
}

名称字段是可编辑的。 Parent 字段仅供引用,不应更新。因此,它是使用 DisplayFor 渲染的。

发布后,我收到一个模型,其属性 Parent 设置为 null。这没问题,因为它不会被保存。但是,当我只是将接收到的模型返回到 View 时,Parent 字段将不再显示。当模型有效时,我可以轻松地从数据库中再次获取它,从而取回 Parent 字段的值。

当模型无效时,我希望允许用户更正输入并尝试再次保存。在那里,我应该使用接收到的输入模型值,但也应该显示显示的值。

实际上,需要显示更多字段以供引用,这些字段通常来自与正在编辑的数据库实体不同的数据库实体。

我看到了将字段作为 View 中的隐藏字段传递的建议,但我非常不愿意从客户端读取不应更新的数据。

有没有比手动将这些值复制到模型中或将它们作为隐藏字段传递更优雅的方法?

最佳答案

将这些不可编辑的属性赋予另一个模型并让它处理这些属性怎么样?

查看模型

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

    [Required]
    public string FirstName { get; set; }

    [Required]
    public string LastName { get; set; }

    public PersonDetailsModel DetailsModel { get; set; }
}

详细模型

public class PersonDetailsModel
{
    public string Mother { get; set; }
    public string Father { get; set; }

    public PersonDetailsModel() { }

    public PersonDetailsModel(int personId)
    {
        // pull required model data from databases
        var db = DBParentContext;
        Mother = db.Parent.Where(m => m.ChildId == personId)
        Father = db.Parent.Where(m => m.ChildId == personId)
    }
}

Controller

public class PersonController : Controller
{
    [HttpPost]
    public ActionResult Edit(PersonViewModel viewModel)
    {
        viewModel.DetailsModel = new PersonDetailsModel(viewModel.Id)

        if (ModelState.IsValid) {
            // ~
        }
        return View(viewModel)
    }
}

查看

@model PersonViewModel

@Html.DisplayFor(m => m.DetailsModel.Mother)
@Html.DisplayFor(m => m.DetailsModel.Father)
@Html.TextBoxFor(m => m.FirstName)
@Html.TextBoxFor(m => m.LastName)

由于像你的母亲这样的细节是不可编辑的,所以它们并不是真正的“编辑”模型的一部分,所以我会把这样的东西排除在外,并尝试让其他东西来处理它们。

关于asp.net-mvc-4 - 完成未绑定(bind)的 MVC 模型属性的最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15706408/

相关文章:

asp.net-mvc-4 - 如何设置在 MVC Razor View 中编辑模式下选中的单选按钮

c# - 模型绑定(bind)程序不适用于 JSON POST

c# - MVC3 POST 模型绑定(bind)不适用于特定的复杂模型

c# - 当我需要使用带有多个参数的 POST 方法而不是单独的模型时?

c# - Action 方法参数的继承

asp.net-mvc-4 - 如何将 Kendo UI Grid 绑定(bind)到 Web API Controller ?

asp.net-mvc - 将 MVC 身份验证与 SignalR v2.x 结合使用

asp.net - ReactJS.NET - bundle - TinyIoCResolutionException : Unable to resolve type: React. IReactEnvironment

c# - 区分应用程序处理程序中的 Web API 请求和 MVC 请求

c# - 在 .NET MVC 中建模绑定(bind) Accept header 的最简洁方法