简体   繁体   English

ViewModel中对象的绑定属性

[英]Binding properties of an object in a ViewModel

Let's say I have this Model: 假设我有这个模型:

public class Person
{
    public string forename { get; set; }
    public string surname { get; set; }
    public string randomValue { get; set; }
}

and this ViewModel: 和这个ViewModel:

public class PersonViewModel
{
    public Person person { get; set; }
    public bool viewSpecificOption { get; set;}
}

Then this controller: 然后这个控制器:

public ActionResult Create([Bind(Include = "person, viewSpecificOption")] PersonViewModel viewModel)
{
    //some code here...
}

You can see I bind the 'Person', but what if I dont want to include the 'Person.randomValue' 您可以看到我绑定了“ Person”,但是如果我不想包含“ Person.randomValue”怎么办

I know I could replace the 'Person' in the viewModel with a string for forename and surname but then I'd have to manually convert it to a actual Person. 我知道我可以用名字和姓氏的字符串替换viewModel中的“ Person”,但随后我必须手动将其转换为实际的Person。

Any suggestions on how I can achieve this is a neat and secure way? 关于如何实现此目标的任何建议是一种干净安全的方法吗?

Thanks to the article @StephenMuecke linked in their comment. 感谢@StephenMuecke在评论中链接的文章。 I'm satisfied with taking the approach of not using bind and just using specific ViewModels. 我对不使用绑定方法而只使用特定的ViewModels感到满意。

But if anyone comes accross this question and has a better way that avoids having to then map the ViewModel to the actual Model, please tell me. 但是,如果有人遇到这个问题,并且有更好的方法避免将ViewModel映射到实际Model,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM