码迷,mamicode.com
首页 > 其他好文 > 详细

排球计分程序(八)——验证编辑方法(Edit method)和编辑视图(Edit view)

时间:2017-06-22 13:27:52      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:entry   orderby   lldb   route   cti   数据   地址栏   发布   包括   

修改代码,使得发布ReleaseDate看上去更好。打开Balls \ Ball.cs文件
using System;

using System.ComponentModel.DataAnnotations;

using System.Data.Entity;

namespace Balls.Models

{

public class Ball{

public int ID { get; set; }

public string Title { get; set; }

[Display(Name = "Release Date")]

[DataType(DataType.Date)]

[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

public DateTime ReleaseDate { get; set; }

public string Shenglv { get; set; }

public string Rongyu { get; set; }

}

public class BallsDBContext : DbContext

{

public DbSet<Ball> Movies { get; set; }

}

}
在浏览器地址栏里追加/Balls, 浏览到Balls页面。并进入编辑(Edit)页面。
                    技术分享

MapRoute方法是使用HTTP请求路由查找到正确的控制器(controller)和行动方法,并提供了可选ID的参数。MapRoute方法也被用于通过HtmlHelpers如ActionLink的控制器,操作方法及任何路由数据,以生成URL。

public static void RegisterRoutes(RouteCollection routes)

{

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(

name: "Default",

url: "{controller}/{action}/{id}",

defaults: new { controller = "Balls", action = "Index",

id = UrlParameter.Optional }

);

}
处理 POST 请求
[HttpPost]

[ValidateAntiForgeryToken]

public ActionResult Edit([Bind(Include="ID,Shenglv,ReleaseDate,Age,Rongyu")] Movie movie)

{

if (ModelState.IsValid)

{

db.Entry(Ball).State = EntityState.Modified;

db.SaveChanges();

return RedirectToAction("Index");

}

return View(Ball);

}
接收form所post的数据,并转换所接收的Balls请求数据从而创建一个Ball对象。ModelState.IsValid方法用于验证提交的表单数据是否可用于修改(编辑或更新)一个Ball对象。如果数据是有效的数据,将保存到数据库的Ball集合(BallDBContext 实例)。通过调用BallDBContext的SaveChanges方法,新的数据会被保存到数据库。数据保存之后,代码会把用户重定向到BallsController类的Index操作方法,页面将显示列表,同时包括刚刚所做的更新。
添加搜索
public ActionResult Index(string BallGenre, string searchString)

{

var GenreLst = new List<string>();

var GenreQry = from d in db.Balls

orderby d.Genre

select d.Genre;

GenreLst.AddRange(GenreQry.Distinct());

ViewBag.movieGenre = new SelectList(GenreLst);

var Balls = from m in db.Movies

select m;

if (!String.IsNullOrEmpty(searchString))

{

Balls = Balls.Where(s => s.Title.Contains(searchString));

}

if (!string.IsNullOrEmpty(BallGenre))

{

Balls = Balls.Where(x => x.Genre == BallGenre);

}

return View(Balls);

}

技术分享

排球计分程序(八)——验证编辑方法(Edit method)和编辑视图(Edit view)

标签:entry   orderby   lldb   route   cti   数据   地址栏   发布   包括   

原文地址:http://www.cnblogs.com/sdl1305702018/p/7064125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!