zoukankan      html  css  js  c++  java
  • ASP.NET MVC Partial View基本使用

    Model:

        public class TestModel
        {
            [Required]
            public string Id { get; set; }
        }

    Partial View(PartialInput.cshtml):

    @model partialview.Models.TestModel
    
    @using (@Html.BeginForm("Index", "Test", FormMethod.Post, new { @id = "form1", @enctype = "multipart/form-data" }))
    {
        <div>
            @*使用ViewData传递数据*@
            <h2>@ViewData["Name"]</h2>
    
            @Html.TextBoxFor(model => model.Id)
            <input type="submit" id="btn" value="Submit">
        </div>
    }

    Index View(Index.cshtml):

    @{
        ViewBag.Title = "Index";
    }
    
    <body>
        @{
            Html.RenderPartial("PartialInput");
        }
    </body>

    Read data from DB in Controller:

        public class TestController : Controller
        {
            // GET: Test
            public ActionResult Index()
            {
                ViewData["Name"] = GetValue(2);
                return View();
            }
    
            [HttpPost]
            public ActionResult Index(TestModel model)
            {
                ViewData["Name"] = GetValue(Convert.ToInt32(model.Id));
                return View();
            }
    
            public string GetValue(int id)
            {
                string connSQL = @"Data Source=(localdb)ProjectsV13;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
    
                using (SqlConnection conn = new SqlConnection(connSQL))
                {
                    string strSQL = "select name from PartialTest where id = " + id.ToString();
                    SqlCommand cmd = new SqlCommand(strSQL, conn);
    
                    conn.Open();
                    if (cmd.ExecuteScalar() == null)
                    {
                        return "NULL";
                    }
                    else
                    {
                        return cmd.ExecuteScalar().ToString();
                    }
                }
            }
        }
  • 相关阅读:
    数据分析优化之惩罚性线性回归算法
    数据分析之贝叶斯算法案例
    SSM-CRUD练习
    使用 Code First 迁移以设定数据库种子
    .Net Web Api相关学习内容
    ASP.NET MVC5的学习知识点
    Entity Framework的学习(ASP.NET MVC5的学习中的一部分)
    EFCore框架的学习
    jsp中的<%%>用法
    nginx出现404和403错误
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/13139860.html
Copyright © 2011-2022 走看看