I work on asp.net MVC application . I face issue I can't pass model ResignationRequester properties values to Approve Index action when click approve button .
all properties as Request no and employee no load when details page load then I write
comment SpeakStuffComment then click approve button to save SpeakStuffComment based on request no .
approve button exist on details page so before click approve button submit
I write comments that I need to save it based on request no then click approve button
so How to pass model ResignationRequester to Approve Index action when click approve button
@model HR.WorkforceRequisition.Models.ResignationRequester
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_LayoutResignation.cshtml";
}
@if (!string.IsNullOrEmpty(ViewBag.msg))
{
@ViewBag.msg
}
@if (ViewBag.AllowApprove == true)
{
using (Html.BeginForm("ApprovalIndex", "Resignation", new { id = Model.RequestNo }, FormMethod.Post, htmlAttributes: new { @style = "display:inline;" }))
{
@Html.AntiForgeryToken()
Approve
}
}
@Html.LabelFor(model => model.RequestNo, htmlAttributes: new { @class = "control-label col-md-5" })
@Model.RequestNo
@Html.LabelFor(model => model.EmpName, htmlAttributes: new { @class = "control-label col-md-5" })
@Model.EmpName
@Html.LabelFor(model => model.EmpID, htmlAttributes: new { @class = "control-label col-md-5" })
@Model.EmpID
@Html.Label("If yes, why did the staff resign? If No, Why? ", htmlAttributes: new { @class = "control-label col-md-5" })
@Html.EditorFor(model => model.SpeakStuffComment, new
{
htmlAttributes = new
{ @class = "form-control" }
})
public class ResignationRequester
{
[Display(Name = "Employee No : ")]
[Required,]
public int EmpID { get; set; }
[Display(Name = "Request No")]
public int RequestNo { get; set; }
[Required]
[Display(Name = "Emp. Name: ")]
public string EmpName { get; set; }
[DataType(DataType.MultilineText)]
public string SpeakStuffComment { get; set; }
}
}
public class ResignationController : Controller
{
[ValidateAntiForgeryToken]
public async Task ApprovalIndex(int id)
{
return new EmptyResult();
}
}
I try make some changes to code above as below but issue all properties values as SpeakStuffComment and Request no show null so
How to solve issue please ?
[ValidateAntiForgeryToken]
public async Task ApprovalIndex(ResignationRequester REQ)
{
return new EmptyResult();
}
using (Html.BeginForm("ApprovalIndex", "Resignation", FormMethod.Post, htmlAttributes: new { @style = "display:inline;" }))
{
@Html.AntiForgeryToken()
Approve
}
from debug break point cached I check properties value of model Resignation Requester it show it null to all properties values.
so what is issue and How to solve it .
Lokesh VarmanPosted Oct 10, 2023, 7:18 AM
If you are experiencing an issue where the `SpeakStuffComment` property is not passing its value to the action method even though you have updated it before clicking the "Approve" button, there could be several reasons for this behavior. Let's explore some common potential causes and solutions:
1. **Form Field Naming:** Ensure that the `SpeakStuffComment` property is correctly bound to the corresponding form field. The `name` attribute of the form field should match the property name in your model. For example:
```html
```
2. **Model Binding:** Verify that you are using the `asp-for` attribute or the `name` attribute correctly for the `SpeakStuffComment` field. The model binder relies on the correct naming to bind data to the model property.
3. **Form Method and Action:** Make sure that you are using the `POST` method in your form, and the action attribute points to the correct action method.
```html
```
4. **JavaScript Interference:** Check if there is any JavaScript code that might be interfering with the form submission or modifying the `SpeakStuffComment` value. Ensure that there are no JavaScript errors in the browser console.
5. **ModelState:** If you are using `ModelState` in your action method, ensure that it is not affecting the value of `SpeakStuffComment`. ModelState can sometimes clear or overwrite field values.
6. **Caching:** Clear your browser cache to make sure you are not dealing with a cached version of the page that might not reflect recent changes.
7. **Validation:** Check if there are any validation attributes or logic on the `SpeakStuffComment` property or the form field that could be causing the value to be rejected during form submission.
8. **Request Data Inspection:** In your action method, you can inspect the request data to see what values are being sent. You can use tools like the built-in Visual Studio debugger or print the data to the console for debugging.
9. **View Model:** Ensure that the `SpeakStuffComment` property is part of the model that you are posting back to the server. If you have a view model, make sure it includes this property.
If you've checked these aspects and the issue persists, consider providing more specific code snippets or additional details about your implementation so that I can offer more targeted assistance.
Note- AI generated
ahmed salahPosted Oct 10, 2023, 7:11 AM
pass all properties model value to action work success
but property SpeakStuffComment still display as null although it have value
this property i add text on it before click approve button update
why this not working please can you help me if possible
Lokesh VarmanPosted Oct 10, 2023, 5:14 AM
It appears that you are trying to pass the
ResignationRequestermodel to theApprovalIndexaction in your ASP.NET MVC application, but the model properties are coming as null. This issue is likely due to how you are setting up the form and handling the form submission.Here's how you can modify your code to pass the
ResignationRequestermodel to theApprovalIndexaction correctly:BeginFormmethod:In the code above, we've added hidden fields for the
EmpIDandRequestNoproperties so that they will be included in the form submission.ApprovalIndexaction to accept the model as a parameter:Make sure that you have the
[HttpPost]attribute on theApprovalIndexaction if it's intended to handle HTTP POST requests.With these changes, the
ResignationRequestermodel should be correctly passed to theApprovalIndexaction when you submit the form, and the properties should have the values you expect.Note - AI generated