vendredi 2 octobre 2015

Application where user inputs month number 1-12 and http post returns month name isn't working

I have an application that asks the user to input a number 1-12 in a form field. Upon hitting a submit button they should be returned to the http post which tells them the corresponding month.

EXAMPLE: if the user inputs '9' the http post SHOULD read "the month you chose is 'September'

As of now, the post is only returning the number again, and not the string/name of the month. I know there are several ways to do this, feel free to show me an easier/quicker one, but I attempted to use if/else if statements. Here is what I have so far (using: visual studio 2013 / MVC-asp.net )

MODEL:

public class IterApp
    {
       public int CurrentMonth { get; set; }

    }

CONTROLLER:

public ActionResult NumberApp()
        {
            IterApp model = new IterApp();
            return View();
        }

        [HttpPost]
        public ActionResult NumberAppResults(IterApp ia)
        {

            return View(ia);
        }

VIEW NumberApp (form view):

<script>

        var CurrentMonth = $("#CurrentMonth").val();

        function whichMonth()
        {
            if(CurrentMonth >= 1)
            {
                CurrentMonth = "January";
            }
            else if(CurrentMonth >= 2)
            {
                CurrentMonth = "February";
            }
            else if(CurrentMonth >= 3)
            {
                CurrentMonth = "March";
            }
            else if(CurrentMonth >= 4)
            {
                CurrentMonth = "April";
            }
            else if(CurrentMonth >= 5)
            {
                CurrentMonth = "May";
            }
            else if(CurrentMonth >= 6)
            {
                CurrentMonth = "June";
            }
            else if(CurrentMonth >= 7)
            {
                CurrentMonth = "July";
            }
            else if(CurrentMonth >= 8)
            {
                CurrentMonth = "August";
            }
            else if(CurrentMonth >= 9)
            {
                CurrentMonth = "September";
            }
            else if(CurrentMonth >= 10)
            {
                CurrentMonth = "October";
            }
            else if(CurrentMonth >= 11)
            {
                CurrentMonth = "November";
            }
            else
            {
                CurrentMonth = "December";
            }
        }



    </script>

    <div>
        <br />
        <form method="post" action="NumberAppResults" onsubmit="return (whichMonth)">
            Let's start a new iteration! This time, enter the NUMBER (1-12) of the month you'd like to output:
            <br />
            <br />
            <input id="CurrentMonth" type="text" name="CurrentMonth" />
            <br/>
            <br />
            <input type="submit" value="Which Month is it?" />

        </form>
    </div>

VIEW NumberAppResults (http post):

<span>The Month you chose is:</span>
    <span>@Model.CurrentMonth</span>

Aucun commentaire:

Enregistrer un commentaire