游戏BOSS提示系统-2设置新建游戏分类和BOSS分类
一、游戏分类
1.在控制器下新建控制器StClassesController

然后选 上连接上下文等

然后会自动的在视图下增加了各种操作页面。

然后调测后,在地址栏后面增加访问为
http://localhost:61752/stclasses
这个时间他第一次使用,会自动firt code,企业管理器中会发现新建了表

二、加入自己的css和js
怎么加上自己的css等哩。
1.在Content 下新建一个css文件,取名jsboss.css

2.在App_Strat下的BundleConfig.cs中修改css的创建
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/jsboss.css",
"~/Content/site.css"));
}
然后保存,生成解决方案。
三、修改STCLASS中的模板
因为游戏版块只有几个,没必要分开设置。
修改VIEW视图中的stclasses下的模板即可。
1.设置新建模板

新建的视图源码
@model stboss.Models.StClass
@{
ViewBag.Title = "Create";
}
<p>
<span class="titleh2">新建游戏</span>
@Html.ActionLink("返回", "Index", "", new { @class = "btn btn-success" })
</p>
<h2></h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.StName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
下面是控制器中Create的内容 ,基本默认,只是增加了一个ID值string的赋值。
// POST: StClasses/Create
// 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关
// 详细信息,请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,StName")] StClass stClass)
{
var dt = DateTime.Now;
string str = dt.ToString("yyyyMMddHHmmss");
stClass.Id = str;
if (ModelState.IsValid)
{
db.StClasses.Add(stClass);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(stClass);
}
然后保存,生成解决方案,调测新建成功!
可我们这里没有用户验证,如何简单的保护进行验证非法修改哩,我想用一个输入与默认字符串对比的功能,如是相同刚表示你是正确的修改。
(1)在Web.config中增加内容 。
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="myString" value="123321" />
</appSettings>
(2)在各模板中增加输入框 与其比较,如是不相等。则转到ViewErroe视图,提示出错。
下面是完整 的新建的视图代码:
@model stboss.Models.StClass
@{
ViewBag.Title = "Create";
}
<p>
<span class="titleh2">新建游戏</span>
@Html.ActionLink("返回", "Index", "", new { @class = "btn btn-success" })
</p>
<h2></h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<label for="exampleInputEmail1" class="control-label col-md-2">游戏名称</label>
<div class="col-md-10">
@Html.EditorFor(model => model.StName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label for="exampleInputName2" class="control-label col-md-2">验证字符</label>
<div class="col-md-10">
<input type="text" class="form-control" id="exampleInputName2" name="exampleInputName2">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
下面是新建的后台代码
// 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关
// 详细信息,请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,StName")] StClass stClass)
{
string string1 = Request["exampleInputName2"];
string string2 = ConfigurationManager.AppSettings["myString"].Trim();
if (string1 != string2)
{
return RedirectToAction("ViewError");
}
var dt = DateTime.Now;
string str = dt.ToString("yyyyMMddHHmmss");
stClass.Id = str;
if (ModelState.IsValid)
{
db.StClasses.Add(stClass);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(stClass);
}
2.设置Edit模板视图
@model stboss.Models.StClass
@{
ViewBag.Title = "Edit";
}
<p>
<span class="titleh2">修改</span>
@Html.ActionLink("返回", "Index", "", new { @class = "btn btn-success" })
</p>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
<label for="exampleInputEmail1" class="control-label col-md-2">游戏名称</label>
<div class="col-md-10">
@Html.EditorFor(model => model.StName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label for="exampleInputName2" class="control-label col-md-2">验证字符</label>
<div class="col-md-10">
<input type="text" class="form-control" id="exampleInputName2" name="exampleInputName2">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
下面是编辑的代码
// POST: StClasses/Edit/5
// 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关
// 详细信息,请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = "Id,StName")] StClass stClass)
{
string string1 = Request["exampleInputName2"];
string string2 = ConfigurationManager.AppSettings["myString"].Trim();
if (string1 != string2)
{
return RedirectToAction("ViewError");
}
///上面是当输入的不等于默认的显示出错。
if (ModelState.IsValid)
{
db.Entry(stClass).State = EntityState.Modified;
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(stClass);
}
3.详细视图,没必要了。
4.设置Index视图
@model IEnumerable<stboss.Models.StClass>
@{
ViewBag.Title = "Index";
}
<p>
<span class="titleh2">游戏列表</span>
@Html.ActionLink("新建", "Create", "", new { @class = "btn btn-success" })
</p>
<table class="table">
<tr>
<th>
游戏名称
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.StName)
</td>
<td>
@Html.ActionLink("修改", "Edit", new { id=item.Id }, new { @class = "btn btn-success btn-sm" }) |
@Html.ActionLink("删", "Delete", new { id = item.Id }, new { @class = "btn btn-warning btn-sm" })
</td>
</tr>
}
</table>
4.删除视图Delete
和新建,修改一样,我都懒得帖了。
4
浙公网安备 33010602011771号