MVC開發之排球計分(七)驗證編輯方法

來源:互聯網
上載者:User

標籤:ace   using   resource   contain   mod   項目   執行個體   lstat   地址欄   

修改代碼,使得發布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 one1{ get; set; }

public string one2 { get; set; }

public string one3{ get; set; }

}

public class BallsDBContext : DbContext

{

public DbSet<Ball> Team { 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 ActionResultone1([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);

 方法的驗證主要是對輸入的得分情況及得分上限的驗證

本次的項目驗證是對排球每一局的得分進行簡單的驗證

MVC開發之排球計分(七)驗證編輯方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.