MVC 个人总结

来源:互联网 发布:手机淘宝返利怎么返利 编辑:程序博客网 时间:2024/06/06 08:37

1.<a href="@Url.Action(".....")">跳转至对应的Controller的Action方法所指向的页面。

如果Action方法存在参数,则<a href="@Url.Action("方法名称",new {id=@model.id...})"


2.button方法按下后执行方法:

@using (Html.BeginForm("PostTable", "Table1", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))

{}

3.获取页面中的textbox中的值:

 @Html.TextBoxFor(model => model.name, new { @class = "col-xs-10 col-sm-5" })
 @Html.ValidationMessageFor(model => model.name)



4.Controller


namespace 1.Controllers
{
    [Authorize]
    public class Table1Controller : Controller
    {
        private Table1Service _Table1Serivce= new Table1Service();
        private ApplicationUserManager _userManager;
        public Table1Controller()
        {
            ServiceFactory<Table1Service>.Current.Create(_Table1Serivce);
        }


        public ApplicationUserManager UserManager
        {
            get
            {
                return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            private set
            {
                _userManager = value;
            }
        }


        // GET: Table1
        public ActionResult Index()//查询
        {
            var list= _Table1Serivce.GetAllTable1();
            var table1 = new List<Table1Model>();
            Table1ListModel model = new Table1ListModel();
            foreach(var tb in list)
            {
                table1.Add(new Table1Model
                    {
                        Id=tb.Id,
                        name = tb.name,
                    });
            }
            model = new Table1ListModel { tablelist = table1 };
            return View(model);
        }


        public ActionResult Deletetable(int id)//删除
        {
            var tb = _Table1Serivce.Gettable1ById(id);


            _Table1Serivce.DeleteTable1(tb);
            return RedirectToAction("Index");
        }


        public ActionResult ViewTable(int id)//显示某个信息
        {
            if (id > 0)
            {


            }
            var table = _Table1Serivce.Gettable1ById(id);
            var model = new Table1Model
            {
                Id=table.Id,
                name=table.name,
            };
            return View(model);
        }
        public ActionResult NewTable()//打开新增页面
        {
            var model = new Table1Model();
            return View(model);
        }
        public ActionResult ViewUpdate(int id)//打开修改页面
        {
            var table = _Table1Serivce.Gettable1ById(id);
            var model = new Table1Model
            {
                Id = table.Id,
                name = table.name,
            };
            return View(model);
        }


        [HttpPost]
        public ActionResult PostTable(Table1Model model)//新增
        {
            if (ModelState.IsValid)
            {
                try
                {
                    int Id = model.Id;
                    string name=model.name;


                    var table1 = new Table1
                    {
                        Id = Id,
                        name = name,
                    };


                    _Table1Serivce.InsertTable1(table1);


                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }


            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult UpdateTable(Table1Model model)//修改
        {


            var table1 = _Table1Serivce.Gettable1ById(model.Id);
            table1.name = model.name;
            _Table1Serivce.UpdateTable1(table1);


            return RedirectToAction("Index");
        }
       
    }
}

0 0
原创粉丝点击