MVC4制作網(wǎng)站教程第四章 更新欄目4.3
序
一、用戶
二、用戶組
三、欄目
3.1添加欄目
3.2瀏覽欄目
3.3更新欄目
上次在樹形列表里面點(diǎn)擊欄目名稱后跳轉(zhuǎn)到詳細(xì)信息頁面~/Category/ManageDetails/id。在詳細(xì)頁面里點(diǎn)修改,來完成欄目資料修改。
先打開【CategoryController】添加[ManageDetails(int id)]action
/// <summary>
/// 欄目詳細(xì)資料
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult ManageDetails(int id)
{
categoryRsy = new CategoryRepository();
var _node = categoryRsy.Find(id);
if (_node == null)
{
Error _e = new Error { Title = "欄目不存在", Details = "欄目不存在", Cause = Server.UrlEncode("<li>欄目已經(jīng)刪除</li>"), Solution = Server.UrlEncode("<li>返回<a href='" + Url.Action("Manage", "Cayegory") + "'>欄目欄目管理</a></li>") };
return RedirectToAction("ManageError", "Prompt", _e);
}
ModuleRepository _moduleRsy = new ModuleRepository();
var _modules = _moduleRsy.List(true);
List<SelectListItem> _slimodule = new List<SelectListItem>(_modules.Count());
foreach (Module _module in _modules)
{
if (_node.Model == _module.Model) _slimodule.Add(new SelectListItem { Text = _module.Name, Value = _module.Model, Selected = true });
else _slimodule.Add(new SelectListItem { Text = _module.Name, Value = _module.Model });
}
ViewData.Add("Model", _slimodule);
var _type = TypeSelectList;
_type.SingleOrDefault(t => t.Value == _node.Type.ToString()).Selected = true;
ViewData.Add("Type", _type);
return View(_node);
}
代碼先是看欄目是否存在,不存在跳轉(zhuǎn)到錯誤頁面,后面是添加"Model“和“Type”的ViewData
右鍵添加強(qiáng)類型視圖ManageDetails.cshtml,內(nèi)容基本與ManageAdd.cshtml類似
@model Ninesky.Models.Category
@{
ViewBag.Title = "欄目信息";
Layout = "~/Views/Layout/_Manage.cshtml";
}
<div class="workspace">
<div class="inside">
<div class="notebar">
<img alt="" src="~/Skins/Default/Manage/Images/Category.gif" />欄目信息
</div>
@using (Html.BeginForm("ManageUpdate","Category"))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>詳細(xì)資料</legend>
<ul>
@Html.HiddenFor(model => model.CategoryId)
<li>
<div class="editor-label">
@Html.LabelFor(model => model.CategoryId)
</div>
<div class="editor-field">
@Html.DisplayTextFor(model => model.CategoryId)
</div>
</li>
<li>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</li>
<li>
<div class="editor-label">
@Html.LabelFor(model => model.ParentId)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.ParentId, new { @class = "easyui-combotree", data_options = "url:'" + Url.Action("JsonTreeParent", "Category") + "'" })
@Html.ValidationMessageFor(model => model.ParentId)
</div>
</li>
<li>
<div class="editor-label">
@Html.LabelFor(model => model.Type)
</div>
<div class="editor-field">
@Html.DropDownList("Type")
@Html.ValidationMessageFor(model => model.Type)
</div>
</li>
<li id="li_model">
<div class="editor-label">
@Html.LabelFor(model => model.Model)
</div>
<div class="editor-field">
@Html.DropDownList("Model")
@Html.ValidationMessageFor(model => model.Model)
</div>
</li>
<li id="li_categoryview">
<div class="editor-label">
@Html.LabelFor(model => model.CategoryView)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CategoryView)
@Html.ValidationMessageFor(model => model.CategoryView)
</div>
</li>
<li id="li_contentview">
<div class="editor-label">
@Html.LabelFor(model => model.ContentView)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ContentView)
@Html.ValidationMessageFor(model => model.ContentView)
</div>
</li>
<li id="li_nav">
<div class="editor-label">
@Html.LabelFor(model => model.Navigation)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Navigation)
@Html.ValidationMessageFor(model => model.Navigation)
</div>
</li>
<li>
<div class="editor-label">
@Html.LabelFor(model => model.Order)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Order)
@Html.ValidationMessageFor(model => model.Order)
</div>
</li>
<li>
<div class="editor-label">
<input id="Submit1" type="submit" value="修改" />
</div>
<div class="editor-field">
</div>
</li>
</ul>
</fieldset>
}
</div>
</div>
<div class="left">
<div class="top"></div>
@Html.Action("ManagePartialTree", "Category")
</div>
<div class="split"></div>
<div class="clear"></div>
<script type="text/javascript">
Details();
$("#Type").change(function () {
Details();
});
function Details() {
var v = $("#Type").val();
if (v == "0") {
$("#li_model").show();
$("#li_categoryview").show();
$("#li_contentview").show();
$("#li_nav").hide();
$("#Navigation").val("");
}
else if (v == "1") {
$("#li_model").hide();
$("#li_categoryview").show();
$("#li_contentview").hide();
$("#ContentView").val("");
$("#li_nav").hide();
$("#Navigation").val("");
}
else if (v == "2") {
$("#li_model").hide();
$("#li_categoryview").hide();
$("#CategoryView").val("");
$("#li_contentview").hide();
$("#ContentView").val("");
$("#li_nav").show();
}
}
</script>
@section Scripts {
@Styles.Render("~/EasyUi/icon")
@Scripts.Render("~/bundles/EasyUi")
@Scripts.Render("~/bundles/jqueryval")
}
注意的是 @using (Html.BeginForm("ManageUpdate","Category"))這句;表示點(diǎn)修改按鈕的后是向ManageUpdate提交數(shù)據(jù)。下面開始做這個action
在【CategoryController】里添加httppost方式的[ManageUpdate]action
/// <summary>
/// 修改欄目信息
/// </summary>
/// <param name="category"></param>
/// <returns></returns>
public ActionResult ManageUpdate(Category category)
{
switch (category.Type)
{
case 0:
category.Navigation = "";
break;
case 1:
category.Model = "";
category.ContentView = "";
category.Navigation = "";
break;
case 2:
category.Model = "";
category.CategoryView = "";
category.ContentView = "";
break;
}
categoryRsy = new CategoryRepository();
if (categoryRsy.Update(category))
{
Notice _n = new Notice { Title = "修改欄目成功", Details = "修改欄目成功!", DwellTime = 5, NavigationName = "欄目詳細(xì)信息", NavigationUrl = Url.Action("ManageDetails", "Category", new { id = category.CategoryId }) };
return RedirectToAction("ManageNotice", "Prompt", _n);
}
else
{
Error _e = new Error { Title = "修改欄目失敗", Details = "在修改欄目信息時,未能保存到數(shù)據(jù)庫", Cause = "系統(tǒng)錯誤", Solution = Server.UrlEncode("<li>返回<a href='" + Url.Action("ManageDetails", "Category", new { id = category.CategoryId }) + "'>欄目詳細(xì)資料</a>頁面,修改信息后重新操作</li><li>聯(lián)系網(wǎng)站管理員</li>") };
return RedirectToAction("ManageError", "Prompt", _e);
}
}
很簡單,首先是判斷欄目類型,根據(jù)欄目類型清除無關(guān)數(shù)據(jù),然后將修改保存到數(shù)據(jù)庫。
試一下將“測試欄目”改成“公司簡介”

保存成功!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- MVC+EasyUI+三層新聞網(wǎng)站建立 建站準(zhǔn)備工作(一)
- MVC+EasyUI+三層新聞網(wǎng)站建立 主頁布局的方法(五)
- MVC+EasyUI+三層新聞網(wǎng)站建立 實(shí)現(xiàn)登錄功能(四)
- MVC+EasyUI+三層新聞網(wǎng)站建立 后臺登錄界面的搭建(二)
- MVC+EasyUI+三層新聞網(wǎng)站建立 驗證碼生成(三)
- 一步步打造簡單的MVC電商網(wǎng)站BooksStore(2)
- 一步步打造簡單的MVC電商網(wǎng)站BooksStore(1)
- MVC4制作網(wǎng)站教程第四章 瀏覽欄目4.2
- MVC4制作網(wǎng)站教程第四章 添加欄目4.1
- MVC+EasyUI+三層新聞網(wǎng)站建立 tabs標(biāo)簽制作方法(六)
相關(guān)文章
如何為CheckBoxList和RadioButtonList添加滾動條
這篇文章主要介紹了為CheckBoxList和RadioButtonList添加滾動條的方法,感興趣的小伙伴們可以參考一下2016-07-07
ADO.NET獲取數(shù)據(jù)(DataSet)同時獲取表的架構(gòu)實(shí)例
下面小編就為大家分享一篇ADO.NET獲取數(shù)據(jù)(DataSet)同時獲取表的架構(gòu)實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
C#頁碼導(dǎo)航顯示及算法實(shí)現(xiàn)代碼
C#頁碼導(dǎo)航算法要求:頁數(shù)小于等于1時不顯示;頁數(shù)大于10時,自動縮短,需要的朋友可以了解下2012-12-12
.net讓線程支持超時的方法實(shí)例和線程在執(zhí)行結(jié)束后銷毀的方法
兩個問題:.net如何讓線程支持超時?.net如何讓線程在執(zhí)行結(jié)束后銷毀?本文就解決這二個問題2013-11-11
ASP.NET Core SignalR中的流式傳輸深入講解
這篇文章主要給大家介紹了關(guān)于ASP.NET Core SignalR中流式傳輸?shù)南嚓P(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11

