简体   繁体   中英

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>

I very well could be looking at this wrong, but I'm not quite sure how you're returning CurrentMonth as a string when it's an int in your class. That said, why not try this?

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

    }

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

    [HttpPost]
    public ActionResult NumberAppResults(IterApp ia)
    {
        //You might need to move this around to where it's appropriate, 
        //but this will return the name of the month for the int value
        //that you receive. The if/else you have should be unnecessary.

        ia.MonthName = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(ia.CurrentMonth);
        return View(ia);
    }

Basically - just add a MonthName property to the class, then set that value using the DateTimeFormat.GetMonthName() function before you return to the view.

You'll also want to update the View to use @Model.MonthName to display the result.

using System.Globalization;

//exemple for january

CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(1);

//just replace 1 with your int variable

First of all, you shouldn't be computing the month name in your view if you're going to post the month integer to your controller. That should be computed in your post action. Second, once you've fixed that, you need to make the model for the view returned by your post action a string, or something that contains a string. You can't say "The Month you chose is: @Model.CurrentMonth" and expect that integer field to contain the string month name.

Your code will never execute past this point:

if(CurrentMonth >= 1)
{
    CurrentMonth = "January";
}
else if(....)

instead you need to do this:

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

function whichMonth(currentMonth)
{
    switch(currentMonth)
    {
        case 1: return "January";
        case 2: return "February";
        case 3: return "March";
        case 4: return "April";
        case 5: return "May";
        case 6: return "June";
        case 7: return "July";
        case 8: return "August";
        case 9: return "September";
        case 10: return "October";
        case 11: return "November";
        case 12: return "December";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM