0
votes

I'm getting this error when I try to hit the submit button on my form. I believe I'm getting this error because I haven't selected anything but I'm really not sure because I populate it when the user loads the form.

Here is my index method:

ViewBag.numOfPages = GetNumberOfPagesList();
        ViewBag.paperStyle = new SelectList(styleList, "Value", "Text");
        ViewBag.urgency = GetUrgencySelectList();
        ViewBag.subject = new SelectList(subjectList, "Value", "Text");
        ViewBag.paperType = GetPaperTypeSelectList();
        ViewBag.price = 0.00;
        ViewBag.spacing = "double";

        return View();

Here is the method that it calls to populate the list:

public SelectList GetPaperTypeSelectList()
    {
        var paperTypes = PaperType.GetPaperTypes();

        return new SelectList(paperTypes.ToArray(), "deadlineFormat", "typeName");
    }

Here is the view code for the select list in question:

<div class="row">
    @Html.DropDownListFor(model => model.paperType, (SelectList)ViewBag.paperType, new { id = "paperTypeJList" })
    @Html.LabelFor(model => model.paperType, "Type of Paper:")
    @Html.ValidationMessageFor(model => model.paperType)
</div>

Here is my model code:

[Required(ErrorMessage = "Full Name is Required.")] public string fullName { get; set; }

    [DataType(DataType.EmailAddress, ErrorMessage = "A Valid Email Address is Required.")]
    [Required(ErrorMessage = "Email Address is Required.")]
    public string email { get; set; }

    [DataType(DataType.PhoneNumber, ErrorMessage = "A Valid Email Address is Required.")]
    [Required(ErrorMessage = "Phone Number is Required.")]
    public string phone { get; set; }

    [Required(ErrorMessage = "Topic is Required.")]
    public string topic { get; set; }

    [Required(ErrorMessage = "Subject is Required.")]
    public string subject { get; set; }

    [Required(ErrorMessage = "Paper Type is Required.")]
    public string paperType { get; set; }

    [Required(ErrorMessage = "Urgency is Required.")]
    public Int16 urgency { get; set; }

    [Required(ErrorMessage = "Number of Pages is Required.")]
    public Int16 numOfPages { get; set; }

    [Required(ErrorMessage = "Requirements are Required.")]
    [DataType(DataType.MultilineText)]
    [StringLength(200)]
    public string requirements { get; set; }

    [Required(ErrorMessage = "Writing Style is Required.")]
    public string style { get; set; }

    [Required(ErrorMessage = "Spacing is Required.")]
    public string spacing { get; set; }

    [Required(ErrorMessage = "Price is required")]
    [Range(0.01, 10000.00, ErrorMessage = "Your quote is not complete because you haven't completed all of the steps.")]
    [DataType(DataType.Currency)]
    public decimal price { get; set; }

Please let me know if you need anymore information. I read other questions about this error and I'm setting the information properly and the code to bind it looks correct as well. This code used to work but I don't remember changing anything.

1

1 Answers

0
votes

you need to call this again in the post action as it will be lost after the form posted and view re-rendered ViewBag values are removed:

    ViewBag.numOfPages = GetNumberOfPagesList();
    ViewBag.paperStyle = new SelectList(styleList, "Value", "Text");
    ViewBag.urgency = GetUrgencySelectList();
    ViewBag.subject = new SelectList(subjectList, "Value", "Text");
    ViewBag.paperType = GetPaperTypeSelectList();