MVC 2 - 使用父/子实体时 HTML 输入字段上的名称属性
我对使用实体框架的 MVC 2 还很陌生。我有两个表 Company {ID int Identity PK,Name nvarchar} 和 User {ID int Identity PK,UserName nvarchar,CompanyID int FK}。用户和公司之间存在外键。
我生成了 ADO.NET 实体数据模型、控制器和用于插入记录的视图。我的 HTML 表单包含“公司”和“用户名”字段,其想法是当我单击“保存公司”时,用户将被插入到数据库中。听起来很直接吧!
我的问题如下:
我创建了一个从“用户”实体派生的强类型视图。我正在使用 html 帮助器 Html.TextBoxFor(model => model.Organization.Name) 但此输入字段的 html 名称属性是 'Organization.Name' >。我的问题是,这个点在 JQuery 中引发了各种问题,JQuery 将此视为一个属性。如果我想更改名称,我读到可以使用 DataAnnotations,但因为我使用了实体设计器,所以这涉及到使用 Buddy 类。仅仅更改此输入字段上的 html 名称属性似乎有点过分了。我是否以正确的方式处理这个问题,或者我在这里遗漏了什么?
感谢您的帮助!
I'm pretty new to MVC 2 using the Entity Framework. I have two tables Company {ID int identity PK,Name nvarchar} and User {ID int identity PK,UserName nvarchar,CompanyID int FK}. A Foreign Key exists between User and Company.
I generated my ADO.NET Entity Data Model, a Controller and a view to insert a record. My HTML form has the fields Company and UserName and the idea is when I click save a Company and User is inserted into the database. Sounds straight forward right!
My question is as follows:
I created a strongly-typed view derived from my 'User' entity. I'm using the the html helper Html.TextBoxFor(model => model.Organisation.Name) but the html name attribute for this input field is 'Organisation.Name'. My problem with this is that the dot throws up all sorts of issues in JQuery, which sees this as a property. If I want to change the name I read that I can use DataAnnotations but because I used the Entity Designer this involves using Buddy Classes. Seems like a bit of overkill just to change the html name attribute on this input field. Am I approaching this the right way or am I missing something here?
Thanks for the help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过后退一步并重新评估我构建数据的方式解决了这个问题。最终结果是我的业务实体与我的数据库模式耦合得太紧密,并且没有反映我正在工作的域。我重新设计了我的应用程序。使用 POCO 来表示更好地反映我的领域的业务实体,这在这种情况下具有“扁平化”关系结构的效果,因此我现在拥有 model.OrganizationName,而不是 model.Organization.Name。
I resolved this by taking a step back and reevaluating the way I was structuring my data. The end result was that my business entities were too closely coupled to my database schema and didn't reflect the domain I was working in. I redesigned my app. using POCO's to represent my business entities that better reflected my domain and this had the effect of 'flattening' the relational structure in this scenario, so instead of model.Organisation.Name I now have model.OrganisationName.