• 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();
                    }
                }
            }
        }
  • 相关阅读:
    eclipse打包
    java reflect 小例子
    linux查看java jdk安装路径和设置环境变量
    mapreduce (一) 物理图解+逻辑图解
    java url 解码 编码 奇怪的解码两次
    cygwin+hadoop+eclipse (三) 运行wordcount实例
    nutch 与 solr 的结合
    一个项目可以有多个源代码路径
    SHAppBarMessage
    记录系统开机启动登录注销等信息
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/13139860.html
Copyright © 2020-2023  润新知