zoukankan      html  css  js  c++  java
  • ASP.NET使用ViewBag和HttpPost在MVC中进行基本数据类型的数据传递Demo

    第一步创建一个空的控制器

    在这里插入图片描述

    在这里插入图片描述

    该控制器的代码为

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace WebApplication1.Controllers
    {
        public class Demo2Controller : Controller
        {
            // GET: Demo2
            public ActionResult Index()
            {
                return View();
            }
    
            //基本类型的绑定
            [HttpPost]							//int?  是该int可以为空值
            public ActionResult Index(string id, int? score, string comment)
            {
                ViewBag.Info = $"{id}提交的评论信息是{comment},评分是{score}";
                return View();
            }
        }
    }
    

    第三步 右键添加视图

    在这里插入图片描述

    在这里插入图片描述

    第四步 Index页面

    
    
    
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <div>
            @*action是提交的页面*@
            <form action="/Demo2/Index" method="post">
                @*name要对应我后台的*@
                <div>账号:<input type="text" name="id" /> </div>
                <div>评分:<input type="text" name="score" /> </div>
                <div>评价:<textarea name="comment" cols="50" rows="10"></textarea> </div>
                <input type="submit" value="提交" />
                <h1>@ViewBag.Info</h1>
            </form>
        </div>
    </body>
    </html>
    
    
    

    效果图

    在这里插入图片描述

    PS:
    如果想提交完还保存原来的值,需要在原文本框添加value属性指定为ViewBag传过来的值
    在这里插入图片描述

  • 相关阅读:
    第一周例行报告
    内置函数_map、filter
    时间戳
    模块_pip、os模块
    常用内置函数
    函数递归、列表推导式
    Python基础(六)_全局变量声明、可变参数、关键字参数
    Python基础(五) 函数
    python基础(四)集合
    Python基础(三)文件操作
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13046995.html
Copyright © 2011-2022 走看看