gpt4 book ai didi

c# - 用户输入月份编号 1-12 并且 http post 返回月份名称的应用程序不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 23:29:02 27 4
gpt4 key购买 nike

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

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

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

型号:

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(表单 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>

查看 NumberAppResults(http 帖子):

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

最佳答案

我很可能看错了,但我不太确定你如何将 CurrentMonth 作为字符串返回,而它在你的类中是一个 int。那就是说,为什么不试试这个呢?

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 属性添加到类中,然后在返回 View 之前使用 DateTimeFormat.GetMonthName() 函数设置该值。

您还需要更新 View 以使用 @Model.MonthName 来显示结果。

关于c# - 用户输入月份编号 1-12 并且 http post 返回月份名称的应用程序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32909522/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com