0

I am writing a basic application using ASP.NET MVC. I have a select dropdown with three text values, blank, "Yes, I'll be there", "No, I can't come". I would like the option value 'true' to be registered by the GuestResponse object in the controller when the "Yes I'll be there" option in the form is submitted, but at the moment it's only coming through as "null". Have I set up the form correctly, and is it sensible to use a boolean type in the model?

GuestResponse.cs (model)

    public class GuestResponse
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public bool? WillAttend { get; set; }
    }

RSVPForm.cshtml

    <form method="post" action="/Home/RsvpForm">
        <div class="form-group">
        <p>
            <label for="Name">Your name:</label>
            <input type="text" id="name" name="name" />
        </p>
        </div>
        <p>
            <label for="Email">Your email:</label>
            <input type="text" id="email" name="email" />
        </p>
        <p>
            <label for="Phone">Your phone:</label>
            <input type="text" id="phone" name="phone" />
        </p>
        <p>
            <label>
                Will you attend?
            </label>
            <select asp-for="WillAttend">
                <option value="Choose an option"></option>
                <option value="true">Yes, I'll be there</option>
                <option value="false">No, I can't come</option>
            </select>
        </p>
        <button type="submit">Submit RSVP</button>

    </form>

HomeController.cs

    public class HomeController : Controller
    {
        [HttpPost]
        public ViewResult RsvpForm(GuestResponse guestResponse)
        {
            Repository.AddResponse(guestResponse); // "WillAttend" should return true here.
            return View("Thanks", guestResponse);
        }
        public ViewResult ListResponses()
        {
            return View(Repository.Responses.Where(r => r.WillAttend == true));
        }
    }

Thanks.cshtml (the Thanks page)


      @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Thanks</title>
    </head>
    <body>
        <p>
            <h1>Thank you, @Model.Name!</h1>
            @if (Model.WillAttend == true) {
                @:It's great that you're coming. The drinks are already in the fridge!
            } else {
                @:Sorry to hear that you can't make it, but thanks for letting us know.
            }
        </p>
        <p>Click @Html.ActionLink("here", "ListResponses") to see who is coming. </p>
    </body>
    </html>

Thanks,

Regards,

Robert

London, UK

2
  • value="Choose an option" is not a bool. Maybe <option value="">Choose an option</option> Commented Aug 29, 2020 at 16:43
  • Try in options WillAttend set values to True, False Commented Aug 29, 2020 at 19:35

1 Answer 1

1

You can also use name attribute to send value to controller.

<select name="WillAttend">
                <option value="Choose an option"></option>
                <option value="true">Yes, I'll be there</option>
                <option value="false">No, I can't come</option>
            </select>
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.