我可以创建一个模型(类)绑定视图然后使用另一个模型将其保存在数据库中吗? [英] Can I Create A Model(Class) For Binding The View And Then Using The Another Model To Save It In Database?

查看:58
本文介绍了我可以创建一个模型(类)绑定视图然后使用另一个模型将其保存在数据库中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我认为可能更好地接受建议而不是开始实施。所以我有一个基本上由实体框架生成的类,该类将用于保存用户的数据。现在我有3个类用于在我的Create.cshtml中实现级联下拉列表。

所以

I have a question which i think might be better to take advice on rather than starting implementing it. So i have a class which is basically Generated By entity framework and that class will be used to save the data from the User. Now i have 3 classes which will be used to implement Cascading Drop down list in my Create.cshtml.
So

public class Form
{
public int Id {get; set;}
public string Country {get; set;}
public string state {get; set;}
public string city {get; set;}
public string Name {get; set;}
public string Email {get; set;}
}

public class Country
{
public int CountId {get; set;}
public string Country {get; set;}
}

public class State
{
public int stateId {get; set;}
public string State {get; set;}
public int CountId {get; set;}
}

public class City
{
public int Id {get; set;}
public string City {get; set;}
public int stateId {get; set;}
}





你可以看到他们的是国家之间的一对多关系 - >州和州 - > city。

Form类主要用于填写用户的表单,就像注册表那样。



现在我创建强类型视图对Form类,但问题是他们无法从他们的字段中获取Country,State,City属性,我不想将字段添加到我的Form类,因为我的数据库只保存这些字段。所以我创建了另一个class ClassForRealform(我所知道的是这种技术称为MVVM或View模型)





As you can see their is one to many relationship between Country --> state and State --> city.
The Form class is basically used to fill the form from the user like a registeration form something of that sort.

Now i create strongly typed View against Form class but the problem is their is no way get the Country, State, City property from their Fields and i do not want to add fields to my Form Class because my database save only these fields.So i created another class ClassForRealform (What i know is this technique is called as MVVM or View model)

public class ClassForRealform 
{
<pre>public int Id {get; set;}
public string Country {get; set;}
public string state {get; set;}
public string city {get; set;}
public string Name {get; set;}
public string Email {get; set;}

public virtual List<Country> countries {get; set;}
public virtual List<state> states {get; set;}
public virtual List<City> cities {get; set;}
}



现在将我的表单绑定到此类



现在我添加一个控制器动作方法让我们说保存



public actionresult保存(表单)

{

//代码保存

}



现在我的问题是我可以这样做吗将我的视图绑定到另一个类并将数据传递给另一个类,从而[填充所需的字段?希望我能在这里说清楚





2.还有一件事情有点不合适

如何使用html.label(,new {id =label1})来放置一个空标签
这样的
但是上面不起作用


Now bind my form to this class

now i add an controller action method let's say "Save"

public actionresult Save(Form form)
{
//Code to save
}

Now my question is can i do this binding my view to another class and passing data to another class and thus [populating the fields required? hope i make myself clear here


2. one more thing thats little out of line
How to place an empty label using html.label("",new {id ="label1" })
like this but above do not work

推荐答案

项目总是应该提供代码优化技术和性能,然后只有应用程序占用服务器的低空间并提供更好的性能。



为此我们什么时候想要使用依赖项相关属性的数量,那么我们应该只维护一个我们在代码中维护的类作为Model。



这个的主要目的是对象初始化,只有单个类才会发生破坏。



所以最好在同一视图中减少相同属性的不同类。
The project always should provide the code optimization techniques and performance then only the application occupy low space in server and provide the better performance.

For that when ever we want to use number of dependency related properties then we should maintain only one class which we have maintained in our code as Model.

The main purpose of this is object initialization and destroying will happen for only single class.

So better to reduce different classes for same properties with in same view.


这篇关于我可以创建一个模型(类)绑定视图然后使用另一个模型将其保存在数据库中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆