“21天好习惯”第一期-3

ASP.NET  MVC—EF实体模型

课程作业项目实践操作过程

操作步骤

1创建MVC项目并取名为“WEIphotoText”

2链接数据库,绑定Model数据

选择Model文件夹,点击鼠标右键添加新建项

 选择数据添加实体类,模型取名为Photo

 点击添加

下一步,新建连接

 选择个人计算机SQL server的数据库服务器名称

选择登录方式,要引用的数据库

测试连接,显示连接成功

 点击确定,下一步

选择实体框架,选择要引用的表,这里选择两个

点击完成

两个表就引用进来了,自动生成了实体模型

 3添加Photo控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WEIphotoText.Models;  //引用模型

namespace WEIphotoText.Controllers
{
    public class PhotoController : Controller
    {
        WEIphotoDBEntities db = new WEIphotoDBEntities(); //创建数据库实体对象
        // GET: Photo
        public ActionResult Index()
        {
            List<Photograph> list = db.Photographs.ToList();
            return View(list);
        }

        // GET: Photo/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Photo/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Photo/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Photo/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Photo/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Photo/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Photo/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

 添加index视图,选择list模板,选择类

@model IEnumerable<WEIphotoText.Models.Photograph>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.photographLevelId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.photographName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LengthPixel)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.widthPixel)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.photographIntroduction)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ShootTime)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.userId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.photographValue)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.downloadRight)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.useRight)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.photographlikeCount)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.downloadCount)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.phoCommentCount)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.phoUploadTime)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.photographIs_Del)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.photographLevelId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.photographName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LengthPixel)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.widthPixel)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.photographIntroduction)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ShootTime)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.userId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.photographValue)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.downloadRight)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.useRight)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.photographlikeCount)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.downloadCount)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.phoCommentCount)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.phoUploadTime)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.photographIs_Del)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.photographId }) |
            @Html.ActionLink("Details", "Details", new { id=item.photographId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.photographId })
        </td>
    </tr>
}

</table>

浏览器中查看

 相关数据表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Redmonster0923

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值