开发者

MVC 2 - Name Attributes on HTML Input Field when using Parent/Child Entities

开发者 https://www.devze.com 2022-12-28 02:33 出处:网络
I\'m pretty new to MVC 2 using the Entity Framework. I have two tables Company {ID intidentity PK,Name nvarchar} and User {ID int identity PK,UserName nvarchar,CompanyID int FK}. A Foreign Key exists

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 !


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号