Home > Back-end >  ModelState Not Valid as Hidden field not binding
ModelState Not Valid as Hidden field not binding

Time:08-24

I'm quite a beginner at MVC and got stuck. I have searched but can't find a solution! I am Posting a Create from my View but the Controller is unable to validate the Model state on the Line If ModelState.IsValid Then I have tracked the issue using

ModelState.Values.SelectMany(Function(v) v.Errors).Select(Function(b) b.ErrorMessage)

and receive this

(0): "The ClientJobID field is required."
(1): "The EmployeeID field is required."

Both of these should be coming from the View's "HiddenFor" lines but seem to pass null for some reason?
My Model is:

    Imports System
    Imports System.Collections.Generic
    
    Partial Public Class JobTimesheetT
        Public Property JobTimesheetID As Integer
        Public Property ClientJobID As Integer
        Public Property EmployeeID As Integer
        Public Property JobTaskID As Nullable(Of Integer)
        Public Property TaskDate As Date
        Public Property TotalHours As Nullable(Of Decimal)           
    
        Public Overridable Property ClientJobT As ClientJobT
        Public Overridable Property EmployeeT As EmployeeT
        Public Overridable Property JobTaskT As JobTaskT    
    End Class

The Controller snippet includes:

    <HttpPost()>
    <ValidateAntiForgeryToken()>
    Function Create(<Bind(Include:="JobTimesheetID,ClientJobID,EmployeeID,JobTaskID,TaskDate)>
                    ByVal jobTimesheetT As JobTimesheetT" ) As ActionResult

        '~~~~~~~~~~~~~~~~~~~~
        'SET RETURN VARIABLES
        '~~~~~~~~~~~~~~~~~~~~
        ViewBag.JobTimesheetID = jobTimesheetT.JobTimesheetID
        ViewBag.ClientJobID = jobTimesheetT.ClientJobID
        ViewBag.EmployeeID = jobTimesheetT.EmployeeID  
        ViewBag.JobTaskID = New SelectList(db.JobTaskTs, "JobTaskID", "JobTaskName", jobTimesheetT.JobTaskID)

        '~~~~~~~~~~~~~~~~~
        'CHECK MODEL STATE
        '~~~~~~~~~~~~~~~~~
        If ModelState.IsValid Then  '... Is not Valid!

And a snippet of the View is:

@ModelType xxx.JobTimesheetT
    @Code
        ViewData("Title") = "Add Time"  'Create
        Layout = "~/Views/Shared/_Layout.vbhtml"
    End Code

<h2>Add Time</h2>
<p> ClientJob id =  @ViewBag.ClientJobID</p>  'Added these to confirm values loaded correctly
<p>Emp = @ViewBag.EmployeeID</p>    

@Using (Html.BeginForm())
    @Html.AntiForgeryToken()
    @Html.HiddenFor(Function(model) model.ClientJobID, ViewBag.ClientJobID)
    @Html.HiddenFor(Function(model) model.EmployeeID, ViewBag.EmployeeID)
    @Html.Hidden("Client", ViewBag.Client)
    @<div  style="Text-align: center; margin: 10px,auto,10px,Auto;">
        <h4>Job Timesheet for: @ViewBag.Client</h4>
        <hr />
        @Html.ValidationSummary(True, "", New With {.class = "text-danger"})

        <h3>Task</h3>
        <div  style="margin: 20px auto; width: 100%; display: inline-block">
            <div >                  
                @Html.DropDownList("JobTaskID", Nothing, "Select a Task", htmlAttributes:=New With {.class = "TimeSheetDrop ", .style = "width: 300px", .required = "required"})
                @Html.ValidationMessageFor(Function(model) model.JobTaskID, "", New With {.class = "text-danger"})
            </div>
        </div>
... More fields here...
...
    
    <div  style="display:inline-block; margin: auto; width:100%; text-align:center">
            <div>
                <input type="Submit" value="Save" Class="list-group-item btn btn-info" style="margin: auto;color: #2c76eb;font-weight: bolder;width:40%; height:40px" />
            </div>
        </div>

    </div>
End Using

So why can't I get these values to bind and save to the Table? I would really appreciate any suggestions you may have please?

CodePudding user response:

Try to pass ViewBag.ClientJobID and ViewBag.EmployeeID value from your controller GET action method sample, E.g. I am posting.

<HttpGet()>
Function Create()
{
    //your code
    ViewBag.ClientJobID="Some value";
    ViewBag.EmployeeID="Some value";

}

CodePudding user response:

Weird, but it seems to have required me to better define the values for the hiddenFor's. Changing to:

    @Html.HiddenFor(Function(model) model.ClientJobID, New With {.Value = ViewBag.ClientJobID})  
    @Html.HiddenFor(Function(model) model.EmployeeID, New With {.Value = ViewBag.EmployeeID})

enabled the ModelState.IsValid to return True.

This now works, but i'm not sure why it didn't before? Perhaps someone can enlighten me?

  • Related