Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.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 如何从现有模型定义视图模型_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 4 如何从现有模型定义视图模型

Asp.net mvc 4 如何从现有模型定义视图模型,asp.net-mvc-4,Asp.net Mvc 4,我想知道如何从下面的类中定义viewmodel public class TestModel { public int Id { get; set; } public string Name { get; set; } public bool HasCompleted { get; set; } public DateTime DeadLine { get; set; } public DateTime? CreatedDate { get; set; }

我想知道如何从下面的类中定义viewmodel

public class TestModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool HasCompleted { get; set; }
    public DateTime DeadLine { get; set; }
    public DateTime? CreatedDate { get; set; }
    public DateTime? LastModified { get; set; }
}
从上面的模型中,只有Id、Name、HasCompleted和Deadline字段会显示给用户。否则,CreatedDate字段和LastModified字段将在内部处理

最初,将使用上述所有字段创建数据库表。但是,如前所述,为了避免过度发布攻击,我创建了一个包含所有必需字段的视图模型。现在,结构如下所示

public class TestModel
{
    public TestVM testVM { get; set; }
    public DateTime? CreatedDate { get; set; }
    public DateTime? LastModified { get; set; }
} 
public class TestVM
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool HasCompleted { get; set; }
    public DateTime DeadLine { get; set; }
}
如果仍然希望维护单个数据库表并执行CRUD操作。但是,我在下面的行动中遇到了障碍

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(TestVM item)
    {            
        //Once the values are bound to TestVM. How do I get the instance of the TestModel to update the LastModified property here??
    }
有人能给点建议吗

问候,,
Ram从TestModel类中删除TestViewModel

public class TestModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool HasCompleted { get; set; }
    public DateTime DeadLine { get; set; }
    public DateTime? CreatedDate { get; set; }
    public DateTime? LastModified { get; set; }
}

public class TestViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool HasCompleted { get; set; }
    public DateTime DeadLine { get; set; }
} 

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(TestViewModel item)
{            
    var testModel = new TestModel 
    {
        Name = item.Name,
        HasCompleted = item.HasCompleted,
        DeadLine  = item.DeadLine 
    };

   //testModel.CreateDate = DateTime.Now;
}
您还可以用于阻止绑定
CreatedDate
LastModified
字段:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Exclude("CreatedDate", "LastModified"))]TestModel item)
{            
}

有人能就此提出建议吗?非常紧急。。