我是MVC的新手。我有一个编辑视图,其中包含与页面菜单相关的一些字段,如名称,URL等。在同一视图中,我将页面角色列表(管理员,客户等)显示为复选框。在“编辑获取请求”中,将根据特定“页面菜单”的数据库值显示一些复选框。
例如,如果管理员和客户有权查看该特定页面,则只会在“编辑获取请求”中选中“管理员和客户角色”复选框。
我有三个表页面,角色和PageRole表连接其他两个表。
现在,如果用户编辑了Page和Checks / Unchecks复选框,我需要有关如何根据编辑后期操作中的复选框选择将数据保存回表格的帮助。
我的编辑视图模型类:
public class PageAdminEditViewModel : PageAdminViewModel
{
public virtual SelectList PageList { get; set; }
public IList<PageRoleData> PageRoleData { get; set; }
}
我的Checkboxes的Model类:
public class PageRoleData
{
public string ASPNetRoleID { get; set; }
public string PageRoleID { get; set; }
public int PageID { get; set; }
public string RoleName { get; set; }
public bool Selected { get; set; }
}
我的编辑获取操作方法:
public ActionResult Edit(int id)
{
if (ModelState.IsValid)
{
try
{
var pData = m_pas.Get<PageAdminEditViewModel>(id);
var data = m_pas.GetAll(true);
SelectList pageList
= new SelectList(data,
nameof(PageAdminEditViewModel.Pageid),
nameof(PageAdminEditViewModel.Pagename));
IList<PageRoleData> pageRoleData = m_pas.GetPageRolesByPageID(id);
pData.PageList = pageList; // For Dropdownlist values
pData.PageRoleData = pageRoleData; // For Checkboxes values
return View(pData);
}
catch (Exception ex)
{
s_log.Fatal($"An error occurred when calling the Edit Page. {ex.Message}", ex);
}
}
return View();
}
我的编辑帖子操作:
[HttpPost]
public ActionResult Edit(int id, PageAdminEditViewModel editViewModel)
{
try
{
m_uow.BeginTransaction();
m_pas.CreateOrEdit(id, editViewModel);
m_uow.Commit();
return RedirectToAction("Index");
}
catch (Exception ex)
{
s_log.Fatal($"An error occurred while saving the changes on the Edit Page. {ex.Message}", ex);
}
return View();
}
发布请求的我的服务层CreateorEdit()方法
public void CreateOrEdit(int id, PageAdminEditViewModel viewModel)
{
Page pageData = null;
// Checks whether to create a new Page
if (id <= 0)
{
pageData = new Page();
}
else
{
pageData = m_uow.QueryFactory.ObjectQuery.Get<Page, int>(id);
}
// Checks if the Page has a Parent Page associated with it
if (pageData.ParentNodeID != null)
{
// Checks if the user has changed the existing Parent page and assigns the new Parent Page accordingly
if (viewModel.ParentNodeID != pageData.ParentNodeID.Pageid)
{
var newParentPage = m_uow.QueryFactory.ObjectQuery.Get<Page, int>(viewModel.ParentNodeID);
pageData.ParentNodeID = newParentPage;
}
}
// In case there is no Parent Page, Checks if the user has set any Parent Page and assigns accordingly
else if (viewModel.ParentNodeID != 0)
{
var newParentPage = m_uow.QueryFactory.ObjectQuery.Get<Page, int>(viewModel.ParentNodeID);
pageData.ParentNodeID = newParentPage;
}
pageData.Pagename = viewModel.Pagename;
if (viewModel.ParentNodeID != 0)
pageData.ParentNodeID.Pageid = viewModel.ParentNodeID;
else
pageData.ParentNodeID = null;
pageData.Url = viewModel.Url;
pageData.Iconimage = viewModel.Iconimage;
pageData.Pagevisibility = viewModel.Pagevisibility;
pageData.Displayorder = viewModel.Displayorder;
var pData = Mapper.Map<Page, PageDto>(pageData);
IList<PageRoleData> selectedRoles = viewModel.PageRoleData;
m_uow.AddOrUpdate(pageData);
}
我的编辑视图:
@model ISPC.CustomerWebsite.Areas.PageAdministration.ViewModels.PageAdminEditViewModel
@{
if (Model.Pageid <= 0)
{
ViewBag.Title = "Create Page Menu";
}
else
{
ViewBag.Title = "Edit Page Menu";
}
}
@if (Model.Pageid <= 0)
{
<h2>Create Page Menu</h2>
}
else
{
<h2>Edit Page Menu</h2>
}
<br />
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="container-fluid" style="float: left; width: 60%;">
<div class="editor-label">
@Html.LabelFor(model => model.Pagename)
<div class="editor-field">
@Html.TextBoxFor(model => model.Pagename, new { style = "width: 200px" })
@Html.ValidationMessageFor(model => model.Pagename)
</div>
</div><br />
<div class="editor-label">
@Html.LabelFor(model => model.ParentNodeID)
<div class="editor-field">
@Html.DropDownListFor(model => model.ParentNodeID, Model.PageList, "-Select-", new { style = "width: 200px" })
@Html.ValidationMessageFor(model => model.Pagename)
</div>
</div><br />
<div class="editor-label">
@Html.LabelFor(model => model.Url)
<div class="editor-field">
@Html.TextBoxFor(model => model.Url, new { style = "width: 200px" })
@Html.ValidationMessageFor(model => model.Url)
</div>
</div><br />
<div class="editor-label">
@Html.LabelFor(model => model.Iconimage)
<div class="editor-field">
@Html.TextBoxFor(model => model.Iconimage, new { style = "width: 200px" })
@Html.ValidationMessageFor(model => model.Iconimage)
</div>
</div><br />
<div class="editor-label">
@Html.LabelFor(model => model.Pagevisibility)
<div class="editor-field">
@Html.EditorFor(model => model.Pagevisibility)
@Html.ValidationMessageFor(model => model.Pagevisibility)
</div>
</div><br />
<div class="editor-label">
@Html.LabelFor(model => model.Displayorder)
<div class="editor-field">
@Html.TextBoxFor(model => model.Displayorder, new { style = "width: 200px" })
@Html.ValidationMessageFor(model => model.Displayorder)
</div>
</div><br />
<div>
@Html.HiddenFor(model => model.Pageid)
</div><br />
<div class="row">
<div>
<input type="submit" value="Save" class="btn btn-default" /> |
@Html.ActionLink("Back to List", "Index")
</div>
</div>
</div><br />
<div class="form-group">
<label class="col-md-pull-2 control-label">
Select User Role :
</label><br />
<div class="col-md-pull-2">
@foreach(var item in Model.PageRoleData)
{
if (item.PageRoleID != null)
{
item.Selected = true;
}
else
{
item.Selected = false;
}
@Html.HiddenFor(model => item.ASPNetRoleID)
@Html.CheckBoxFor(model => item.Selected, new { @id = "chkbox1", @class = "form-check" })
<text> </text>
@Html.LabelFor(model => item.RoleName, item.RoleName)<br />
}
</div>
</div>
}