I work on asp.net MVC . I need to update last working date
based on request no when value Last working date changed .
I need to using ajax jQuery to do that
Web API I will call it using jQuery ajax is Resignation/Edit
Resignation is controller and Edit is action and I will pass
object to Edit action result include request no and last working date .
my code as below :
C#
public class ResignationRequester
{
[Display(Name = "Request No")]
public int RequestNo { get; set; }
[Required]
[Display(Name = "Last Working Day: ")]
[DataType(DataType.Date, ErrorMessage = "Date only")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime LastWorkingDate { get; set; }
}
//post
[ValidateAntiForgeryToken]
public async Task Edit(ResignationRequester req)
{
//UPDATE last working date based on Request No
}
@model HR.WorkforceRequisition.Models.ResignationRequester
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_LayoutResignation.cshtml";
}
@Html.LabelFor(model => model.RequestNo, htmlAttributes: new { @class = "control-label col-md-5" })
@Model.RequestNo
@Html.LabelFor(model => model.LastWorkingDate, htmlAttributes: new { @class = "control-label col-md-5" })
@Html.EditorFor(model => model.LastWorkingDate, new { htmlAttributes = new { @class = "form-control" } })
Tahir AnsariRank #366 · 6.8K reputationPosted Nov 5, 2023, 1:04 AM EST
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(ResignationRequester req) { try { // Your logic to update the Last Working Date based on Request No // Assuming you have some data context to work with (e.g., Entity Framework) using (YourDbContext db = new YourDbContext()) { var resignation = db.Resignations.SingleOrDefault(r => r.RequestNo == req.RequestNo); if (resignation != null) { resignation.LastWorkingDate = req.LastWorkingDate; db.SaveChanges(); } else { // Handle the case where the requested Request No is not found. return Json(new { success = false, message = "Request No not found." }); } } return Json(new { success = true, message = "Last Working Date updated successfully." }); } catch (Exception ex) { // Handle exceptions or errors return Json(new { success = false, message = "An error occurred while updating the Last Working Date." }); } }