首页 > 解决方案 > ASP .Net MVC 简单任务

问题描述

我对一项任务有疑问:当用户进入主页时,请查看表单以输入数字和按钮“回答”
当用户给出数字并按下按钮后,用户会收到信息:

这是一个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真的,我不知道怎么做,我做了一个按钮,但我不知道应该在哪里以及如何做一个检查数字的功能>>

标签: c#asp.netmodel-view-controller

解决方案


如果这正是您想要的,但我补充说;

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


    }

推荐阅读