0

Working on my first ASP.NET MVC application and having some form validation issue.

I have my model:

public class InfoFormEmplModel
{
    public int supID { get; set; }
    public string description { get; set; }

    public InfoFormEmplModel() {}

}

Note that this model doesn't represent any table in my databse.

Now, in my view:

@using Portal.Models
@model InfoFormEmplModel
@{
    ViewBag.Title = "Form";
 }


@using (Html.BeginForm())
{
    <b>Sup</b> @Html.TextBoxFor(x => x.supID)

    <p>Description</p>
    @Html.TextAreaFor(x => x.description)<br><br>

    <input type="submit" name="Save" value="Soumettre" />
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

I need to make some validation, the fields must not be empty and I also have to check if the supId provided exists in my database (server side validation)

I tried to add some validation to my model:

public class InfoFormEmplModel
    {
        [Required (ErrorMessage = "Superior ID required")]
        public int supID { get; set; }

        [Required (ErrorMessage = "Description required")]
        public string description { get; set; }

        public InfoFormEmplModel() {}        
    }

and also added @Html.ValidationMessageFor to my view:

@using Portal.Models
    @model InfoFormEmplModel
    @{
        ViewBag.Title = "Form";
     }


    @using (Html.BeginForm())
    {
        <b>Sup</b> @Html.TextBoxFor(x => x.supID)
        @Html.ValidationMessageFor(x => x.supID)

        <p>Description</p>
        @Html.TextAreaFor(x => x.description)<br><br>
        @Html.ValidationMessageFor(x => x.description)

        <input type="submit" name="Save" value="Soumettre" />
    }

    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }

My controller looks like this:

[HttpPost]
public PartialViewResult invform(InfoFormEmplModel form)
    {

        //check if supID exists
        bool exists = librairie.supExists(form.supID);
        if (!exists)
        {
            return PartialView("ErreurDuplicat");
        }

        return PartialView("Success");
    }

When I leave supID empty, the validation doesn't seem to occur..My controller sends my model to another class that check if the superieur Id is in the DB but supID doesn't have any value. I was expecting that before the controller proceeds, I would see the error message on the web page..

Also, once I checked if the supID exists in the DB, how can I display an error message in my view so the user can enter a valid supID?

4
  • The model you're using in your view is not the same model you're applying the validations to. It should be @model InfoFormulaireEmployeModele. Commented Sep 15, 2016 at 20:04
  • Oops, I corrected that. I made some change to the code so it's smaller and doesn't have a bunch of french variables Commented Sep 15, 2016 at 20:09
  • Still, are you sure this is the code you're using? Your properties are named differently in your view and in your model. You just need to add the code to your current model, view and controller to avoid any confusion. Commented Sep 15, 2016 at 20:14
  • Have you also include jquery{version}.js? Commented Sep 15, 2016 at 21:53

1 Answer 1

2

Assuming you are using always the same view model (and you translated and shortened for clarity) you should be getting your view model in your post action. Then you can use the ModelState property to check whether the received model is valid according to your validation annotations.

If your model is valid you do the server side check for the SupId and if you want to set an error if such Id already exists you can do it as in the following snippet:

[HttpPost]
    public ActionResult invform(InfoFormEmplModel form)
    {
        if (ModelState.IsValid)
        {
            //set an error when the id exists    
            ModelState.AddModelError("supId", "The Id is already in use. Please chose a different Id");
            return View(form);
        }

        return View(form);
    }

For the other error is not possible you are receiving a null id because it's an int. So maybe are you missing something else?

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.