用户输入月号1-12和http post返回月份名称的应用程序不起作用

时间:2015-10-02 14:31:04

标签: c# asp.net-mvc if-statement model-view-controller visual-studio-2013

我有一个应用程序,要求用户在表单字段中输入数字1-12。点击提交按钮后,他们应该返回到http帖子,告诉他们相应的月份。

示例:如果用户输入'9',http帖子应该显示为“您选择的月份是'9月'

截至目前,该帖子仅返回该号码,而不是该月份的字符串/名称。我知道有几种方法可以做到这一点,随时向我展示一个更简单/更快的方法,但我尝试使用if / else if语句。这是我到目前为止(使用:visual studio 2013 / MVC-asp.net)

MODEL:

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

    }

控制器:

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

        [HttpPost]
        public ActionResult NumberAppResults(IterApp ia)
        {

            return View(ia);
        }

VIEW NumberApp(表单视图):

<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>

查看NumberAppResults(http post):

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

4 个答案:

答案 0 :(得分:2)

我很可能会看到这个错误,但我不太确定当你的班级成为一个int时,你是如何将"2015-09-15T21:00:01.562" 作为字符串返回的。那说,为什么不试试呢?

CurrentMonth

基本上 - 只需在类中添加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); } 属性,然后在返回视图之前使用MonthName函数设置该值。

您还希望更新视图以使用DateTimeFormat.GetMonthName()来显示结果。

答案 1 :(得分:1)

using System.Globalization;

//例如1月

CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(1);

//只需用你的int变量

替换1

答案 2 :(得分:0)

首先,如果您要将月份整数发布到控制器,则不应在视图中计算月份名称。这应该在你的后期行动中计算出来。其次,一旦你修复了它,你需要为你的帖子操作返回的视图模型一个字符串,或者包含字符串的东西。你不能说“你选择的月是:@ Model.CurrentMonth”并期望整数字段包含字符串月份名称。

答案 3 :(得分:0)

您的代码永远不会执行这一点:

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

而你需要这样做:

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";
    }
}