自己总结的MVC3增删改查基础方法

1)       Index列表展示一  返回DataTable

[return View() 里边不带参数,前台直接遍历ViewBag.table.Rows]

Controllers 后台

                   ///产品列表

                   public ActionResult Index(int page)

        {

                            page = page > 0 ? page : 1;

                            int recordCount = 0;

                            ViewBag.table = _pModel.Query(page, 10, out recordCount);

            return View();

2)               }

 前台页面:

@model IEnumerable<WholesaleSite.Entities.Product>

 

<h2>产品列表</h2>

<table>

         <tr>

                   <th>产品ID</th>

                   <th>产品名称</th>

                   <th>发布时间</th>

                   <th colspan="2">Operation</th>

         </tr>

         <tr>

         @foreach (var row in ViewBag.table.Rows)

          {

                   <td>@row.ProductId</td>

                   <td>@row.ProductName</td>

                   <td>@row.OperateDate</td>

                   <td>

                            @Html.ActionLink("查看", "Details", new { productId = row.ProductId })

@Html.ActionLink("修改", "Edit", new { productId = row.ProductId })

                            @Html.ActionLink("Delete", "delete", new { productId = row.ProductId })

                   </td>

          }

         </tr>

</table>

3)       Index列表展示二  返回IList

Controllers 后台

public ActionResult Index(int page)

         {

                   int recordCount = 0;

                   IList merchantList = _Model.Query(page, 15, out recordCount);

                   return View(merchantList);

          }

前台页面:

@Model IList<WholesaleSite.DAL.MerchantModel />

@{

    ViewBag.Title = "Index";

}

<h2>商家列表</h2>

<table>

         <tr>

         <th>用户名</th>

         <th>商家名称</th>

         <th>联系方式</th>

         <th>电话</th>

         <th>手机号</th>

         <th>注册时间</th>

         <th colspan="2">Operation</th>

         </tr>

          @foreach (var merchant in Model)

         { 

         <tr>

                   <td>@merchant.UserName</td>

                   <td>@merchant.MerchantName</td>

                   <td>@merchant.Contact</td>

                   <td>@merchant.Phone</td>

                   <td>@merchant.CellPhone</td>

                   <td>@merchant.Operatedate</td>

                   <td>

                            @Html.ActionLink("Edit", "Edit", new { id = merchant.MerchantId })

                            @Html.ActionLink("Details", "Details", new { id = merchant.MerchantId })

                            @Html.ActionLink("Delete", "Delete", new { id = merchant.MerchantId })

                   </td>

         </tr>

          }

</table>

4)       删除的方法

//

                   // GET: /Merchant/Delete/5

 

                   public ActionResult Delete(int  id)

                   {                         

                                     return View();                     

                   }

 

                   //

                   // POST: /Merchant/Delete/5

 

                   [HttpPost]

                   public ActionResult Delete(string id)

                   {

                            try

                            {

                                     _Model.Delete(id);

                                     return RedirectToAction("Index");

                            }

                            catch

                            {

                                     return View();

                            }

                   }

4)修改/添加

   后台:

                   /// <summary>

                   /// 修改/添加

                   /// </summary>    

        public ActionResult Edit(string id)

        {

                            FriendLink friendLink = _fModel.Get(id);

                            return View(friendLink);

        }

 

        //

        // POST: /FriendLink/Edit/5

 

        [HttpPost]

        public ActionResult Edit(int id, FriendLink friendLink)

                   {                         

            try

            {

                                     _fModel.Save(friendLink);

                                     return RedirectToAction("Index", "FirendLink");

            }

                            catch (Exception e)

 

            {

                                     ModelState.AddModelError("", e.Message);              

            }

                            return View();

        }

前台:

@model WholesaleSite.Entities.FriendLink

@{

    ViewBag.Title = "Edit";

}

<h2>Edit</h2>

@using (Html.BeginForm()) {

<fieldset>

         <legend>友情链接#if(Model.LinkId=="")修改#else修改#end</legend>

         <div>

                   @Html.TextBoxFor(Model => Model.LinkId)

         </div>

         <div>

                   友情链接:@Html.TextBoxFor(Model => Model.LinkName)

         </div>

         <div>

                   友情链接地址:@Html.TextBoxFor(Model => Model.LinkUrl)

         </div>

         <p><input type="submit" value="Save"/></p>

</fieldset>

}

 

<div>

         @Html.ActionLink("Back to List","Index")

</div>

 

5)显示详细页

 后台:

       /// 详细页

       /// </summary>    

        public ActionResult Details(string id)

        {

                            FriendLink friendLink = _fModel.Get(id);

                            return View(friendLink);

        }

前台页面:

@model WholesaleSite.Entities.FriendLink

@{

    ViewBag.Title = "Details";

}

<h2>FriendLink Details</h2>

<ul>

         <li>@Model.LinkName</li>

         <li>@Model.LinkUrl</li>

         <li>@Model.Operatedate</li>

</ul>

7) 增删改查已经总结完毕!基础语法:

 

1.       向前台输出列表

a.  DataTable数据集: ViewBag.table = _nModel.Query(page, 10, out recordCount);

                                  return View();

b.  IList数据集:       IList merchantList = _Model.Query(page, 15, out recordCount);

                                  return View(merchantList);

c.  输出对象:                 FriendLink friendLink = _fModel.Get(id);

                                  return View(friendLink);

 

2.       前台绑定

a.后台输出DataTable数据集:

        @model IEnumerable<WholesaleSite.Entities.News>

@foreach (var row in ViewBag.table.Rows)

         {

            @row.Title

}

b.后台输出IList数据集页面上边声明:

@Model IList<WholesaleSite.DAL.MerchantModel />  指向Model

@{

    ViewBag.Title = "Index";     指向输出IList数据集的方法名

}

遍历输出:

@foreach (var merchant in Model)

         { 

                   @merchant.UserName

                   @merchant.Email

}

c.对象输出

@model  WholesaleSite.Entities.Merchant

@{

                  ViewBag.Title = "Details";

}

@Model.UserName

@Model.Email

 

 

3.       跳转页面

web跳转页面

@Html.ActionLink("Edit", "Edit", new { id = friendLink.LinkId })

                    @Html.ActionLink("Details", "Details", new { id = friendLink.LinkId })

                    @Html.ActionLink("Delete", "Delete", new { id = friendLink.LinkId })

后台跳转页面

1.  return RedirectToAction("Index");   直接指向Url,比如Index.cshtml

 

2.  return RedirectToAction("Index","Merchant");  指向Merchant文件夹下的Index.cshtml

 

3.

 if (flag)

                   Response.Write("<script type='text/javascript>alert('删除成功!');window.location.href='index';</script>'");         

      else

                 Response.Write("<script type='text/javascript>alert('删除失败!');window.location.href='index';</script>'");     弹出信息提示框,然后返回到Index.cshtml页面

 

4.       Controllers下的ActionResult Create 方法:

     public ActionResult Create()

        {

            return View();

        }

Create方法是从控制器动作中添加视图

例如:

1.    在代码编辑区中右键Create()方法,选择 Add View.

2.    将复选框Create a strongly-typed view勾上.

3.    View content 下拉框中, 选择Create.

4.    View data class 下拉框中, 选择MovieApp.Models.Movie.

5.    点击Add 按钮创建新视图.

 

MVC3给我们提供的另外一种方便:

 

修改现有数据记录:

首先, 我们需要生成Edit表单.既然Visual Studio会自动为我们生成Edit表单,那么这一步就很简单了。在Visual Studio代码编辑区打开HomeController.cs 并参照以下步骤:

1.    在代码编辑区域右键Edit()方法,选择Add View (见图14).

2.    复选框Create a strongly-typed view勾选上.

3.    View content 下拉框中, 选择Edit.

4.     View data class 下拉框中, 选择MovieApp.Models.Movie.

5.    点击Add 按钮添加新视图.

完成这些步骤以后一个名为Edit.aspx的新视图将会被创建到Views\Home 目录下. 这个视图包含了用来修改数据记录的表单。

每个函数的用途:

·         Index() – 显示Task列表的时候调用

·         Create() – 添加新Task的时候调用

·         CreateNew() – 提交新task的时候调用. 这个控制器行为会将新task添加到数据库中.

·         Complete() – 结束task的时候调用.

 

在控制器行为中需要添加逻辑代码以实现我们想要的功能。

 

posted on 2011-08-22 17:06  落叶十九  阅读(3805)  评论(1编辑  收藏  举报