简体   繁体   中英

Single entity with single view or two views in mvc3 vs2010?

I have the following entity model

public class Employee
{
    public int Employee ID{get;set;}

    public string employeename{get;set;}

    public datetime employeeDOb{get;set;}

    public datetime? employeeDateOfJoin{get;set;}

    public string empFamilyname{get;set;}

    public datetime empFamilyDob{get;set;}    
}

here I have to design a view for collecting employee information and employee family information.

Since I am working on already available data, where in empFamilyDob was not mandatory. But now it is being made mandatory, the previous data doesn't contain EmpFamilyDob. So naturally I have added this new property EmpFamilyDob to the Model and made it required through DataAnnotations.

Now there are two set of views to be developed. 1. A view which simply allows to collect the employee information without employee family information. ie, empFamilyName and EmpFamilyDob .--This view is used by the Hr section to insert empplyee details

  1. Since the empFamilyname and EmpFamilyDob being now made mandatory, some other section will edit the data and update the EmpFamilyName and EmpFamilyDob as and when the information about employee family details are received.

I have action controller for CreateNew and Edit Which is being generated by using the default model. There are two user actions being performed.

1.When the user clicks the Create new -- he will be able to update only the Employee information

2.As and when the other section receives the employee family details they update the familyname and family date of birth. ie, EmployeeFamilyname and EmployeFamilyDob.

While creating new record the uses should be able to update employee information only and while editing the information he should be able to update the employeefamily information. Since I have a single view with most of these fields as required and not allowing null , How can I achieve this in a sincle view?

I have recorrected the model like this

public class Employee {

public int Employee ID{get;set;}

public string employeename{get;set;}

public datetime employeeDOb{get;set;}

public datetime? employeeDateOfJoin{get;set;}

public string empFamilyname{get;set;}

public datetime? empFamilyDob{get;set;}    

}

Now by default I hope the createnew action would insert null value for empFamilyname(string datatype) and empFamilyDob . In the Edit action the user should be made to enter empFamilyname and empFamilyDob(mandatory). As there is every chance that the user might edit other information about the employee(like employeeDob) I don't want to go for partial views. Can you help me out with some illustration. Thanks in advance

"Create new" action prevents users from submitting family informations, but they're mandatory, how will it be validated ? Do you specify some default values ?

Anyway, you can use partial views for your need :

  1. _EmployeeInformation will contain fields for employee information only (without family infos)
  2. _EmpFamilyInformation will contains fields related to family only

This way, you can make a "Create new" view containing only "_EmployeeInformation" , while "Edit" view contains "_EmployeeInformation" + _EmpFamilyInformation". This prevents you from duplicating code if you go for two views, or adding some noise in your view (like putting all fields then testing if you can display family info according to the user request) if you go for one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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