欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MVC学习之三_____跳转到编辑页提交信息

程序员文章站 2022-03-12 21:39:16
...
模板页代码
@model PagedList<AppBasicData>
@using Webdiyer.WebControls.Mvc;
@{

}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>IndexPage</title>
    <link rel="stylesheet" type="text/css" href="~/Content/BasicData/div_table.css" />
</head>
<body>
    <div> 
        <div class="table">
            <div class="tab_tr">
                    <div class="tab_th">名称</div>
                    <div class="tab_th">key值</div>
                    <div class="tab_th">value值</div>
                    <div class="tab_th">创建时间</div>
                    <div class="tab_th">操作</div>
            </div>
            
                @foreach (var item in Model)
                {
                    <div class="tab_tr">
                        <div class="tab-td">@item.Name</div>
                        <div class="tab-td">@item.Key</div>
                        <div class="tab-td">@item.Value</div>
                        <div class="tab-td">@item.CreateDate</div>
                        <div class="tab-td">@Html.ActionLink("编辑", "Edit",new {key = @item.Key}) |@Html.ActionLink("删除", "Delete", new { id = @item.Key})</div>
                    </div>
                }
        </div>
        <div class="text-right">
            @Html.Pager(Model).Options(o => o.SetPageIndexParameterName("id").SetHorizontalAlign("right").SetPagerItemTemplate("{0}&nbsp;"))
        </div>
    </div>
</body>
</html>


编辑页面的模板页代码
@model BasicDataPageModel
@using Manager.Models.BasicData;
@{
    //Layout = null;   这句是不要加载默认模板
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>编辑</title>
    <link rel="stylesheet" type="text/css" href="~/Content/BasicData/div_table.css" />
</head>
<body>
    <br /><br />
    <form action="/BasicData/EditForm" method="post">
        <div class="table">
            <div class="tab_tr">
                <div class="tab-td">名称</div>
                <div class="tab-td"><input name="name" type="text" value="@Model.name" /></div>
            </div>
            <div class="tab_tr">
                <div class="tab-td">关键字</div>
                <div class="tab-td"><input name="key" type="text" value="@Model.key" readonly="readonly" /></div>
            </div>
            <div class="tab_tr">
                <div class="tab-td">值</div>
                <div class="tab-td"><input name="value" type="text" value="@Model.values" /></div>
            </div>
            <div class="tab_tr">
                <div class="tab-td">日期</div>
                <div class="tab-td"><input name="create_date" type="text" value="@Model.create_date" /></div>
            </div>
        </div>
        <div style="text-align:center">
            <br />
            <input type="submit" value="提交" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="重置" />
        </div>
    </form>
        

</body>
</html>



备注:@Html.ActionLink("编辑", "Edit",new {key = @item.Key}) |@Html.ActionLink("删除", "Delete", new { id = @item.Key})
这句是实现跳转传值的设置,new {key = @item.Key}可以将key传递给具体的编辑页面
URL地址为xxxxx/xx/Eidt?key=xxxxx   
new { id = @item.Key})传递后url表现形式是:xxxxx/xxx/xxxxx  默认ID省略了

Controller代码:
public void EditForm()
        {
            string name = Request.Form["name"].ToString();
            string key = Request.Form["key"].ToString();
            string value = Request.Form["value"].ToString();
            string create_date = Request.Form["create_date"].ToString();

            using (var db = new MyDbContext())
            {
                try
                {
                    AppBasicData model = db.AppBasicDatas.Where(x => x.Key == key).SingleOrDefault();
                    model.CreateDate = DateTime.Now;
                    model.Name = name;
                    model.Value = value;
                    db.SaveChanges();
                    Response.Write("<script>alert('保存成功');self.location='/BasicData/IndexPage';</script>"); //给提示后,重定位到列表页
                }
                catch
                {
                    Response.Write("<script>alert('保存失败');self.location='/BasicData/IndexPage';</script>");  //给提示后,重定位到列表页
                }
            }
        }
相关标签: mvc