MVC3增删改查基础方法

来源:互联网 发布:爱淘宝抽一元红包 编辑:程序博客网 时间:2024/05/01 17:00

<!--[if !supportLists]-->1)      <!--[endif]-->Index列表展示一  返回DataTable

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

Controllers 后台

                   ///产品列表

                   publicActionResult Index(intpage)

        {

                            page = page > 0 ? page : 1;

                            int recordCount = 0;

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

            returnView();

<!--[if !supportLists]-->2)      <!--[endif]-->        }

前台页面:

@model IEnumerable<WholesaleSite.Entities.Product>

<h2>产品列表</h2>

<table>

         <tr>

                   <th>产品ID</th>

                   <th>产品名称</th>

                   <th>发布时间</th>

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

         </tr>

         <tr>

         @foreach (var rowinViewBag.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>

<!--[if !supportLists]-->3)      <!--[endif]-->Index列表展示二  返回IList

Controllers 后台

public ActionResult Index(int page)

         {

                   intrecordCount = 0;

                   IListmerchantList = _Model.Query(page, 15,outrecordCount);

                   returnView(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>

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

         </tr>

          @foreach(var merchantinModel)

         {

         <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>

<!--[if !supportLists]-->4)      <!--[endif]-->删除的方法

//

                   //GET: /Merchant/Delete/5

                   publicActionResult Delete(int  id)

                   {                         

                                     return View();                     

                   }

                   //

                   // POST: /Merchant/Delete/5

                   [HttpPost]

                   publicActionResult Delete(stringid)

                   {

                            try

                            {

                                     _Model.Delete(id);

                                     return RedirectToAction("Index");

                            }

                            catch

                            {

                                     return View();

                            }

                   }

4)修改/添加

  后台:

                   ///<summary>

                   ///修改/添加

                   ///</summary>

        public ActionResult Edit(stringid)

        {

                            FriendLink friendLink = _fModel.Get(id);

                            return View(friendLink);

        }

        //

        // POST: /FriendLink/Edit/5

        [HttpPost]

        public ActionResult Edit(intid, FriendLink friendLink)

                   {                         

            try

            {

                                     _fModel.Save(friendLink);

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

            }

                            catch (Exceptione)

            {

                                     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><inputtype="submit" value="Save"/></p>

</fieldset>

}

<div>

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

</div>

5)显示详细页

后台:

       ///详细页

       ///</summary>

        public ActionResult Details(stringid)

        {

                            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) 增删改查已经总结完毕!基础语法:

<!--[if !supportLists]-->1.      <!--[endif]-->向前台输出列表

<!--[if !supportLists]-->a. <!--[endif]-->DataTable数据集: ViewBag.table = _nModel.Query(page, 10,out recordCount);

                                  returnView();

<!--[if !supportLists]-->b. <!--[endif]-->IList数据集:       IList merchantList = _Model.Query(page, 15, out recordCount);

                                  returnView(merchantList);

<!--[if !supportLists]-->c. <!--[endif]-->输出对象:                  FriendLinkfriendLink = _fModel.Get(id);

                                  return View(friendLink);

<!--[if !supportLists]-->2.      <!--[endif]-->前台绑定

a.后台输出DataTable数据集:

       @model IEnumerable<WholesaleSite.Entities.News>

@foreach (var rowin ViewBag.table.Rows)

         {

            @row.Title

}

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

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

@{

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

}

遍历输出:

@foreach (var merchantin Model)

         {

                   @merchant.UserName

                   @merchant.Email

}

c.对象输出

@model WholesaleSite.Entities.Merchant

@{

                  ViewBag.Title = "Details";

}

@Model.UserName

@Model.Email

<!--[if !supportLists]-->3.      <!--[endif]-->跳转页面

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页面

<!--[if !supportLists]-->4.       <!--[endif]-->Controllers下的ActionResult Create 方法:

     public ActionResult Create()

        {

            returnView();

        }

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

例如:

<!--[if !supportLists]-->1.   <!--[endif]-->在代码编辑区中右键Create()方法,选择Add View.

<!--[if !supportLists]-->2.   <!--[endif]-->将复选框Create a strongly-typed view勾上.

<!--[if !supportLists]-->3.   <!--[endif]-->View content下拉框中,选择Create.

<!--[if !supportLists]-->4.   <!--[endif]-->View data class下拉框中,选择MovieApp.Models.Movie.

<!--[if !supportLists]-->5.   <!--[endif]-->点击Add按钮创建新视图.

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

修改现有数据记录:

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

<!--[if !supportLists]-->1.   <!--[endif]-->在代码编辑区域右键Edit()方法,选择Add View(见图14).

<!--[if !supportLists]-->2.   <!--[endif]-->复选框Create a strongly-typed view勾选上.

<!--[if !supportLists]-->3.   <!--[endif]-->View content下拉框中,选择Edit.

<!--[if !supportLists]-->4.   <!--[endif]-->View data class下拉框中,选择MovieApp.Models.Movie.

<!--[if !supportLists]-->5.   <!--[endif]-->点击Add按钮添加新视图.

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

每个函数的用途:

<!--[if !supportLists]-->·        <!--[endif]-->Index()显示Task列表的时候调用

<!--[if !supportLists]-->·        <!--[endif]-->Create()添加新Task的时候调用

<!--[if !supportLists]-->·        <!--[endif]-->CreateNew()提交新task的时候调用.这个控制器行为会将新task添加到数据库中.

<!--[if !supportLists]-->·        <!--[endif]-->Complete()结束task的时候调用.

原创粉丝点击