2013-08-29 54 views
1

你好,這是我的模型(或)屬性字段中MVC4如何從mvc4模型中只取特定字段?

public class LeaveModel : IModel<LeaveModel> 
{ 
    public int Id { get; set; } 

    public int userId { get; set; } 

    public string givenName { get; set; } 

    public string shortName { get; set; } 

    public string leaveType { get; set; } 

    public string leaveDescription { get; set; } 

    public string fromDate { get; set; } 

    public string toDate { get; set; } 

    public int noOfDays { get; set; } 

    public string reason { get; set; } 

    public string status { get; set; } 

    public string statusDescription { get; set; } 

    public string createdDate { get; set; } 

    public string modifiedDate { get; set; } 

    public int leaveTypeId { get; set; } 

    public int companyDataId { get; set; } 
    } 

,這是我的控制器,用於新增和更新

[HttpPost] 

    public HttpResponseMessage Post(LeaveModel vm) 
    { 
     if (ModelState.IsValid) 
     { 
      HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, Ileave.Add(vm)); 
      return response; 
     } 
     else 
     { 
      return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); 
     } 
    } 

    [HttpPut] 

    public HttpResponseMessage Put(LeaveModel vm) 
    { 
     Ileave.Update(vm); 
     return Request.CreateResponse(HttpStatusCode.OK); 
    } 

,這是數據庫的交互代碼

public bool Add(LeaveModel vm) 

    { 
     try 
     { 
      vm.createdDate = vm.modifiedDate = DateTime.Now.ToString(); 
      IDbCommand cmd = Db.GetCommand("sp_LeaveApply", CommandType.StoredProcedure); 
      cmd.ParamIn("@LeaveTypeId", vm.leaveTypeId.ToString(), DbType.Int32); 
      cmd.ParamIn("@fromDate", vm.fromDate, DbType.String); 
      cmd.ParamIn("@toDate", vm.toDate, DbType.String); 
      cmd.ParamIn("@noOfDays", vm.noOfDays.ToString(), DbType.Int32); 
      cmd.ParamIn("@reason", vm.reason, DbType.String); 
      cmd.ParamIn("@createdDate", vm.createdDate, DbType.String); 
      cmd.ParamIn("@modifiedDate", vm.modifiedDate, DbType.String); 
      Db.OpenConnection(); 
      int i = cmd.ExecuteNonQuery(); 
      if (i == 1) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
     finally 
     { 
      Db.CloseConnection(); 
     } 

    } 

    public bool Update(LeaveModel vm) 
    { 
     try 
     { 
      vm.modifiedDate = DateTime.Now.ToString(); 
      IDbCommand cmd = Db.GetCommand("sp_LeaveUpdate", CommandType.StoredProcedure); 
      cmd.ParamIn("@Id", vm.Id.ToString(), DbType.Int32); 
      cmd.ParamIn("@leaveTypeId", vm.leaveTypeId.ToString(), DbType.Int32); 
      cmd.ParamIn("@fromDate", vm.fromDate, DbType.String); 
      cmd.ParamIn("@toDate", vm.toDate, DbType.String); 
      cmd.ParamIn("@noOfDays", vm.noOfDays.ToString(), DbType.Int32); 
      cmd.ParamIn("@reason", vm.reason, DbType.String); 
      Db.OpenConnection(); 
      int i = cmd.ExecuteNonQuery(); 
      if (i == 1) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
     finally 
     { 
      Db.CloseConnection(); 
     } 
    } 

看到這裏的問題是在添加時我需要所有的領域和更新的時間我只需要某些領域被更新..對於添加和更新我指 `同樣的模型調用(LeaveModel)..所以這裏添加是好的,但更新時得到像DataReaderhas Toomany字段的錯誤... 我知道爲什麼會出現這個錯誤,但我如何只需要某些領域,而更新這是問題..

請幫助提前

+0

研究使用視圖模型,而不是直接針對您的域模型編碼視圖/ –

回答

1

我建議你實現一個viewmodel類來幫助你。例如,對於更新,您可以創建一個新的類,像這樣:

public class UpdateViewModel 
{ 
    public string givenName { get; set;} 
} 

public ActionMethod Put() 
{ 
    var original = GetOriginalLeaveModelSomehow(); 
    var viewModel = new UpdateViewModel(); 

    viewModel.givenName = original.givenName; 
    // The idea is that the viewModel class contains only the fields you want to display to the user. 

    return View(viewModel); 
} 

[HttpPost] 
public ActionMethod Put(UpdateViewModel viewModel) 
{ 
    var original = GetOriginalLeaveModelSomehow(); 

    originalMode.givenName = viewModel.givenName; 

    Ileave.Update(original); 
} 

我不知道,如果上面的代碼在功能上是正確的,但使用ViewModel類的想法是基本點在這裏。它會讓你的生活變得更加輕鬆,因爲你可以更加靈活地瞭解數據如何顯示給用戶;甚至應該顯示哪些數據。

0

多虧了這一點,最好的辦法是實際上有兩個不同的視圖模型。一個用於添加LeaveModels,一個用於編輯它們。像那樣,編輯模型只能有你想編輯的值。

相關問題