I work on asp.net mvc . i face issue i can't get value of @Model.lastworkingdate.tostring("dd/MM/yyyy") on submit function java script i don't know how to get it value I can get value of lastworkingdate when it display as EditorFor but if it display as property model @Model.lastworkingdate i can't get value on submit function java script so how to get it value on submit function java script when Editor For element not exist
so my logic will be submit function of last working date
if(element editor for exist )
{
ResignationRequester.LastWorkingDate=get value of edit for last working date
}
else
{
ResignationRequester.LastWorkingDate=get value of @Model.lastworkingdate.tostring("dd/MM/yyyy")
}
what i try as below
@Html.LabelFor(model => model.LastWorkingDate, htmlAttributes: new { @class = "control-label col-md-5" })
@if (Session[BLL.Common.SessionKeys.RoleCode].ToString() == "REQ")
{
if (Model.DirectManagerStatus == "Approve" || Model.DirectManagerStatus == "Rejected by Head Department")
{
@Model.LastWorkingDate.ToString("dd/MM/yyyy")
}
else
{
@Html.EditorFor(model => model.LastWorkingDate, new { htmlAttributes = new { @class = "form-control", @id = "LastWorkingDate" } })
}
}
function submit() {
console.log("check submit");
var ResignationRequester = new Object();
var LastWorkingDateElement = document.getElementById("LastWorkingDate");
if (LastWorkingDateElement) {
ResignationRequester.LastWorkingDate = LastWorkingDateElement.value;
} else {
ResignationRequester.LastWorkingDate = "";
console.log("empty")
}
}
Prasad RaveendranPosted Jan 24, 2024, 1:41 AM
It seems you want to handle the LastWorkingDate value differently based on whether an EditorFor element exists or not. To achieve this, you can modify your submit function as follows:
In this modified code, if the EditorFor element exists, it retrieves the value from the element. If it does not exist, it uses the value from
@Model.LastWorkingDate.ToString("dd/MM/yyyy")directly. ThelastWorkingDateModelValueis a string representation of the last working date in the specified format.Make sure this JavaScript code is within a Razor view so that the
@Model.LastWorkingDateis evaluated correctly. Also, ensure that thesubmitfunction is called at the appropriate time in your application flow.Anoop Kumar SharmaPosted Jan 23, 2024, 4:01 AM
Hi Ahmed,
You either have to keep the value in the hidden field or wrap it in a html tag with an id which you can access it through javascript. After that try to access that id's value through the javascript code.
I hope this will help you.