Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 完成MVC模型属性的最佳实践是';不受约束?_Asp.net Mvc 4_Model Binding - Fatal编程技术网

Asp.net mvc 4 完成MVC模型属性的最佳实践是';不受约束?

Asp.net mvc 4 完成MVC模型属性的最佳实践是';不受约束?,asp.net-mvc-4,model-binding,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; } } public cla

我正试图找到最好的方法来使用MVC的模型,只有部分编辑

下面是一个简单的例子

型号

using System.ComponentModel.DataAnnotations;
public class SimpleModel
{
    public int Id { get; set; }
    public string Parent { get; set; }
    [Required]
    public string Name { get; set; }
}
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)
    }
}
查看

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

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

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 }; }
    }
}
public class PersonController : Controller
{
    [HttpPost]
    public ActionResult Edit(PersonViewModel viewModel)
    {
        viewModel.DetailsModel = new PersonDetailsModel(viewModel.Id)

        if (ModelState.IsValid) {
            // ~
        }
        return View(viewModel)
    }
}
“名称”字段是可编辑的。父字段仅供参考,不应更新。因此,它是使用DisplayFor渲染的

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

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

实际上,要显示更多字段供参考,通常来自不同的数据库实体,而不是正在编辑的数据库实体

我已经看到过将字段作为视图中的隐藏字段传递的建议,但是我觉得非常不愿意从客户端读取不应该更新的数据


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

将这些不可编辑的属性提供给另一个模型并让它处理这些属性怎么样

查看模型

using System.ComponentModel.DataAnnotations;
public class SimpleModel
{
    public int Id { get; set; }
    public string Parent { get; set; }
    [Required]
    public string Name { get; set; }
}
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)
    }
}
详细型号

using System.ComponentModel.DataAnnotations;
public class SimpleModel
{
    public int Id { get; set; }
    public string Parent { get; set; }
    [Required]
    public string Name { get; set; }
}
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)
    }
}
控制器

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 }; }
    }
}
public class PersonController : Controller
{
    [HttpPost]
    public ActionResult Edit(PersonViewModel viewModel)
    {
        viewModel.DetailsModel = new PersonDetailsModel(viewModel.Id)

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

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

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

因为像你母亲这样的细节是不可编辑的,所以它们实际上不是“编辑”模型的一部分,所以我想把它们放在盒子里,让其他的东西来处理它们。

如果你不打算更新父字段,那么它是否是隐藏的其实并不重要,因为你不会在发布时更新它


在这种情况下,我会使用隐藏字段,只是确保不更新该字段。

这根本不能解决他的问题。他不想每次都从数据库中获取信息。实际上,我并不介意从数据库中提取信息。我只是在寻找一种避免复制属性等的方法。实际上,我只是缺少Webforms的viewstate:-(这个答案看起来是将可编辑值与参考值分开的一种方便方法。将参考值的模型添加为编辑模型的属性是允许强类型视图的一种好方法,而不需要从视图状态中提取数据。谢谢。@MystereMan从我的理解来看,这个问题是将关注点分开设置不可编辑的属性,这样一个模型就不能处理所有属性。OP说
我可以很容易地从数据库中再次获取它
。两天前我在工作中遇到了基本相同的问题,我决定每次都从数据库中提取信息,这样我就可以确定信息是实时的。只是建议^^