ASP .Net MVC简单任务

时间:2019-03-07 15:40:31

标签: c# asp.net model-view-controller

我在一项任务上遇到问题:当用户进入主页时,看到输入数字和按钮“答案”的表格
用户输入号码和按钮后,用户将收到信息:

  • 如果数字为偶数,则为“是”
  • 如果数字不均匀,则“否”

那是Index.cshtml

@model WebApplication10.Models.PersonModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th colspan="2" align="center">Give number</th>
            </tr>
            <tr>
               
                <td>
                    @Html.TextBoxFor(m => m.Number)
                </td>
            </tr>
            
            <tr>
                <td></td>
                <td><input type="submit" value="Answer" /></td>
            </tr>
        </table>
    }
</body>
</html>

这是我的PersonModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication10.Models
{
    public class PersonModel
    {

        ///<summary>
        /// Gets or sets PersonId.
        ///</summary>
        public int Number { get; set; }
       
        
        
    }
}

HomeController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication10.Models;

namespace WebApplication10.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
       [ HttpPost]
    public ActionResult Index(PersonModel person)
        {
            return View(person);

            
        }

       
    }
}

我是ASP .Net MVC的初学者,实际上我并不怎么做,我做了一个按钮bu,但我不知道该在哪里以及如何执行将要检查数字的功能>>

1 个答案:

答案 0 :(得分:0)

Idk,如果这正是您想要的,但是我添加了;

   <td>
                @Html.TextBoxFor(m => m.Number)
                @Html.DisplayFor(m => m.IsEven)
   </td>

  public class PersonModel
    {

        ///<summary>
        /// Gets or sets PersonId.
        ///</summary>
        public int Number { get; set; }
        public string IsEven { get { return Number % 2 == 0 ? "Even" : "Odd"; } }


    }