1

嗨,这是 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)..所以在这里add 很好,但是在更新时出现错误,例如 DataReaderhas Toomany 字段...我知道为什么会出现此错误,但是在更新此问题时如何仅获取某些字段..

请帮忙

提前致谢

4

2 回答 2

1

我建议你实现一个视图模型类来帮助你。例如,对于更新,您可以像这样创建一个新类:

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 类的想法是这里的基本点。这将使您的生活更轻松,因为您可以更灵活地向用户显示数据;甚至应该显示哪些数据。

于 2013-08-29T12:29:11.300 回答
0

最好的方法是实际上有两个不同的视图模型。一种用于添加 LeaveModel,一种用于编辑它们。像这样,编辑模型只能具有您要编辑的值。

于 2013-08-29T12:27:09.060 回答