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();
                    }
                }
            }
        }
  • 相关阅读:
    第三次作业--团队展示(团队)
    第二次作业——个人项目实战
    软件工程实践2017第一次作业-准备
    Calculator PartⅢ
    C++课堂作业2016.05.04
    Masonry 等间隔或等宽高排列多个控件
    iOS绘图框架CoreGraphics分析
    UINavigationControlle 之 UINavigationBar及navigationItem关系探讨
    iOS
    iOS-加载数据的实现-MJRefresh
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/13139860.html
Copyright © 2011-2022 走看看