How can i take only particular fields from the model in mvc4?
Hi this is My Model(or)Property Field in 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; }
}
And This is my Controller For Add and Update
[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);
}
And This is Database interaction codes
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();
}
}
See the problem here is at the time of adding i need all the fields and
the the time of updating i need only certain fields to be updated..for
both Add and Update am refering ``same Model Called (LeaveModel)..so here
add is fine but while updating am getting error like DataReaderhas Toomany
fields... I know why this error came but how can i take only certain
fields while updating this is the problem..
Please Help
Thanks in Advance
No comments:
Post a Comment