问题描述
我写使用MVC脚手架的解决方案。它首先使用实体框架V6和数据库是设计。基础数据模型(ServiceAccountFilter)由数据模型(FilterModel)扩展。有一些逻辑在FilterModel但性质都是从基础模型继承。它仿佛在控制器上使用示范基地坚持。当我改变返回使用FilterModel,我得到的视图需要基本模型,然后在FilterModel的功能是不是在视图中显示一个错误?不知道如何来处理呢?
基本型号:
命名空间FHN.ARX.AdminWeb
{
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel; 公共部分类ServiceAccountFilter
{
[显示名称(过滤器ID)]
公共虚拟INT FilterId {搞定;组; }
[显示名称(过滤器名称)]
公共虚拟字符串FILTERNAME {搞定;组; }
[显示名称(服务帐户)]
公共虚拟字符串ServiceAccount {搞定;组; }
[DisplayName的(文件类型)
公共虚拟字符串的DocType {搞定;组; }
[显示名称(文档类型ID)]
公众可空虚拟LT&;&INT GT; DocTypeId {搞定;组; }
[DisplayName的(文件名ID)]
公共虚拟字符串DocNameId {搞定;组; }
[显示名称(最后修改者ID)]
公共虚拟字符串LastModifiedById {搞定;组; }
[显示名称(最后修改者)]
公共虚拟字符串LastModifiedByName {搞定;组; }
[显示名称(最后修改)]
公众可空虚拟LT&;&System.DateTime的GT;上次更改{搞定;组; }
[显示名称(月回归)]
公众可空虚拟LT&;&INT GT; MonthsToReturn {搞定;组; }
}
}
FilterModel
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Linq的;
使用的System.Web;
使用System.Web.Mvc;命名空间FHN.ARX.AdminWeb.Models
{
公共类FilterModel:ServiceAccountFilter
{
[显示名称(过滤器ID)]
公共覆盖INT FilterId {搞定;组; }
[显示名称(过滤器名称)]
公众覆盖字符串FILTERNAME {搞定;组; }
[显示名称(服务帐户)]
公众覆盖字符串ServiceAccount {搞定;组; }
[DisplayName的(文件类型)
公共重写字符串的DocType {搞定;组; }
[显示名称(文档类型ID)]
公共覆盖可空< INT> DocTypeId {搞定;组; }
[DisplayName的(文件名ID)]
公众覆盖字符串DocNameId {搞定;组; }
[显示名称(最后修改者ID)]
公众覆盖字符串LastModifiedById {搞定;组; }
[显示名称(最后修改者)]
公众覆盖字符串LastModifiedByName {搞定;组; }
[显示名称(最后修改)]
公共覆盖可空<&System.DateTime的GT;上次更改{搞定;组; }
[显示名称(月回归)]
公共覆盖可空< INT> MonthsToReturn {搞定;组; }
公共BOOL选择{搞定;组; }
公共字符串复选框{搞定;组; } 公共IEnumerable的< SelectListItem> DocTypesList(字符串ID)
{
使用(VAR DB =新ARXEntities())
{
VAR DOCTYPE =新的List< SelectListItem>();
DOCTYPE =(从db.vwMapDocNamesToSecurityUsersŤ
其中,t.UserDN == ID
排序依据t.DocType
选择新的{t.DocType,t.DocTypeId})是不同的()选择(X =方式>新SelectListItem(){文字= x.DocType,值= x.DocTypeId.ToString()})排序依据(X = > x.Text).ToList();
返回DOCTYPE;
}
} 公共IEnumerable的< SelectListItem> DocNamesList()
{
使用(VAR DB =新ARXEntities())
{
IEnumerable的< SelectListItem> DOCNAME =新的List< SelectListItem>();
VAR与loggedInUser =C2693; // HttpContext.Current.User.Identity.Name.Split('\\\\')[1];
DOCNAME =(从db.vwMapDocNamesToSecurityUsersŤ
其中,t.UserDN ==与loggedInUser
选择新的{t.DocName,t.DocNameId,t.DocTypeId})是不同的()选择(X =方式>新SelectListItem()。
{
文= x.DocName,
值= x.DocNameId.ToString(),
组=新SelectListGroup(){名称= x.DocTypeId.ToString()}
})鲜明()的OrderBy(X =方式>。x.Text).ToList();
变种docCount = docName.Count();
返回DOCNAME;
}
} 公共IEnumerable的< SelectListItem> ServiceAccountList()
{
使用(VAR DB =新ARXEntities())
{
VAR SA =新的List< SelectListItem>();
SA =(从db.vwMapDocNamesToSecurityUsersŤ
其中,t.UserDN.StartsWith(SA_)
排序依据t.UserDN
选择新的{} t.UserDN)鲜明的()选择(X =方式>新SelectListItem(){文字= x.UserDN,值= x.UserDN})排序依据(X =方式> x.Text).ToList ();
返回SA;
}
} 公共IEnumerable的< SelectListItem> DocNamesByDocTypeIdList()
{
使用(VAR DB =新ARXEntities())
{
IEnumerable的< SelectListItem> DOCNAME =新的List< SelectListItem>();
DOCNAME =(从db.vwMapDocNamesToSecurityUsersŤ
选择新的{t.DocName,t.DocNameId,t.DocTypeId})是不同的()选择(X =方式>新SelectListItem()。
{
文= x.DocName,
值= x.DocNameId.ToString(),
组=新SelectListGroup(){名称= x.DocTypeId.ToString()}
})鲜明()的OrderBy(X =方式>。x.Text).ToList();
变种docCount = docName.Count();
返回DOCNAME;
}
} 公共IEnumerable的< SelectListItem> GetDocNamesForFilterId(INT?ID)
{
使用(VAR DB =新ARXEntities())
{
IEnumerable的< SelectListItem> DOCNAME =新的List< SelectListItem>();
DOCNAME =(从db.ServiceAccountFiltersŤ
其中,t.FilterId == ID
选择新的{t.DocNameId,t.FilterId})是不同的()选择(X =方式>新SelectListItem()。
{
文= x.DocNameId,
值= x.DocNameId.ToString(),
组=新SelectListGroup(){名称= x.DocNameId.ToString()}
})鲜明()的OrderBy(X =方式>。x.Text).ToList();
返回DOCNAME;
}
}
}
}
控制器编辑动作
公众的ActionResult编辑(INT?ID)
{
如果(ID == NULL)
{
VAR SAF =新FilterModel();
返回查看(SAF);
}
FilterModel serviceAccountFilter =(FilterModel)db.ServiceAccountFilters.Find(ID); < ----试过铸造这里,但仍然没有工作。
如果(serviceAccountFilter == NULL)
{
返回HttpNotFound();
}
返回查看(serviceAccountFilter);
}
编辑视图
@model FHN.ARX.AdminWeb.Models.FilterModel@ {
ViewBag.Title =编辑过滤器
布局=〜/查看/共享/ _Layout.cshtml
}< DIV CLASS =PAGETITLE过滤器过滤维护和LT; / DIV>
@using(Html.BeginForm(新{ID =filterForm}))
{
@ Html.AntiForgeryToken() < DIV CLASS =形横>
<小时/>
@ Html.ValidationSummary(真,新{@class =TEXT-危险})
@ Html.HiddenFor(型号=> model.FilterId)
@ Html.HiddenFor(型号=> model.LastModified)
@ Html.HiddenFor(型号=> model.LastModifiedById)
@ Html.HiddenFor(型号=> model.LastModifiedByName) < DIV CLASS =ddlGroups,btmMarg-15>
@ Html.DropDownListFor(M = GT; m.ServiceAccount,Model.ServiceAccountList(),选择服务帐户)
< / DIV>
< DIV CLASS =ddlGroups,COL-MD-10,btmMarg-15>
@ Html.LabelFor(型号=> model.ServiceAccount,htmlAttributes:新{@class =控制标签COL-MD-2})
@ Html.EditorFor(型号=> model.ServiceAccount,新{htmlAttributes = {新@class =表格控}})
< / DIV>
< DIV CLASS =ddlGroups,COL-MD-10,btmMarg-15>
@ Html.LabelFor(型号=> model.DocTypeId,htmlAttributes:新{@class =控制标签COL-MD-2})
@ Html.EditorFor(型号=> model.DocTypeId,新{htmlAttributes = {新@class =表格控}})
< / DIV>
&下,P类=leftMarg-15文本默认ID =docNamesHdrText>选择文档名称被包括在滤波器下; / P>
< DIV CLASS =ckDocNames>
@foreach(在Model.DocNamesByDocTypeIdList变种DN())
{
< DIV CLASS =checkboxContainer>
&LT;输入类=ckBoxes类型=复选框NAME =DocNameIdVALUE =@ dn.ValueDTID =@ dn.Group.Name/>@dn.Text<br /&GT;
&LT; / DIV&GT;
}
&LT; / DIV&GT;
&LT; DIV CLASS =表单组,COL-MD-10,btmMarg-15ID =monthsGroup&GT;
@ Html.LabelFor(型号=&GT; model.MonthsToReturn,htmlAttributes:新{@class =控制标签COL-MD-2})
@ Html.EditorFor(型号=&GT; model.MonthsToReturn,新{htmlAttributes = {新@class =表格控}})
@ Html.ValidationMessageFor(型号=&GT; model.MonthsToReturn,新{@class =TEXT-危险})
&LT; / DIV&GT;
&LT; DIV CLASS =表单组,COL-MD-10,btmMarg-15ID =filterNameGroup&GT;
@ Html.LabelFor(型号=&GT; model.FilterName,htmlAttributes:新{@class =控制标签COL-MD-2})
@ Html.EditorFor(型号=&GT; model.FilterName,新{htmlAttributes = {新@class =表格控}})
@ Html.ValidationMessageFor(型号=&GT; model.FilterName,新{@class =TEXT-危险})
&LT; / DIV&GT;
&LT; BR /&GT;
&LT; DIV CLASS =表单组&GT;
&LT; DIV CLASS =COL-MD-偏移2 COL-MD-10&GT;
&LT;输入类型=提交值=保存级=BTN BTN-默认ID =modSaveButton/&GT;
&LT; / DIV&GT;
&LT; / DIV&GT;
&LT; / DIV&GT;
}&LT; DIV&GT;
@ Html.ActionLink(返回列表,ActiveFilters)|
@ Html.ActionLink(管理主页,指数)
&LT; / DIV&GT;@section脚本{
@ Scripts.Render(〜/包/ jqueryval)
}
这是用在你的情况下,视图模型的例子...(可能是它不是prefect只是用自己的方式暗示)
命名空间FHN.ARX.AdminWeb
{
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel; 公共部分类ServiceAccountFilter
{
公众诠释FilterId {搞定;组; }
公共字符串FILTERNAME {搞定;组; }
公共字符串ServiceAccount {搞定;组; }
公共字符串的DocType {搞定;组; }
公众可空&LT; INT&GT; DocTypeId {搞定;组; }
公共字符串DocNameId {搞定;组; }
公共字符串LastModifiedById {搞定;组; }
公共字符串LastModifiedByName {搞定;组; }
公众可空&LT;&System.DateTime的GT;上次更改{搞定;组; }
公众可空&LT; INT&GT; MonthsToReturn {搞定;组; }
}
}
现在,取而代之的是
无需过滤模型把所有的逻辑控制器中的heper在区域
#区域助手公共IEnumerable的&LT; SelectListItem&GT; DocTypesList(字符串ID)
{
使用(VAR DB =新ARXEntities())
{
VAR DOCTYPE =新的List&LT; SelectListItem&GT;();
DOCTYPE =(从db.vwMapDocNamesToSecurityUsersŤ
其中,t.UserDN == ID
排序依据t.DocType
选择新的{t.DocType,t.DocTypeId})是不同的()选择(X =方式&gt;新SelectListItem(){文字= x.DocType,值= x.DocTypeId.ToString()})排序依据(X = &GT; x.Text).ToList();
返回DOCTYPE;
}
}公共IEnumerable的&LT; SelectListItem&GT; DocNamesList()
{
使用(VAR DB =新ARXEntities())
{
IEnumerable的&LT; SelectListItem&GT; DOCNAME =新的List&LT; SelectListItem&GT;();
VAR与loggedInUser =C2693; // HttpContext.Current.User.Identity.Name.Split('\\\\')[1];
DOCNAME =(从db.vwMapDocNamesToSecurityUsersŤ
其中,t.UserDN ==与loggedInUser
选择新的{t.DocName,t.DocNameId,t.DocTypeId})是不同的()选择(X =方式&gt;新SelectListItem()。
{
文= x.DocName,
值= x.DocNameId.ToString(),
组=新SelectListGroup(){名称= x.DocTypeId.ToString()}
})鲜明()的OrderBy(X =方式&gt;。x.Text).ToList();
变种docCount = docName.Count();
返回DOCNAME;
}
}公共IEnumerable的&LT; SelectListItem&GT; ServiceAccountList()
{
使用(VAR DB =新ARXEntities())
{
VAR SA =新的List&LT; SelectListItem&GT;();
SA =(从db.vwMapDocNamesToSecurityUsersŤ
其中,t.UserDN.StartsWith(SA_)
排序依据t.UserDN
选择新的{} t.UserDN)鲜明的()选择(X =方式&gt;新SelectListItem(){文字= x.UserDN,值= x.UserDN})排序依据(X =方式&gt; x.Text).ToList ();
返回SA;
}
}公共IEnumerable的&LT; SelectListItem&GT; DocNamesByDocTypeIdList()
{
使用(VAR DB =新ARXEntities())
{
IEnumerable的&LT; SelectListItem&GT; DOCNAME =新的List&LT; SelectListItem&GT;();
DOCNAME =(从db.vwMapDocNamesToSecurityUsersŤ
选择新的{t.DocName,t.DocNameId,t.DocTypeId})是不同的()选择(X =方式&gt;新SelectListItem()。
{
文= x.DocName,
值= x.DocNameId.ToString(),
组=新SelectListGroup(){名称= x.DocTypeId.ToString()}
})鲜明()的OrderBy(X =方式&gt;。x.Text).ToList();
变种docCount = docName.Count();
返回DOCNAME;
}
}公共IEnumerable的&LT; SelectListItem&GT; GetDocNamesForFilterId(INT?ID)
{
使用(VAR DB =新ARXEntities())
{
IEnumerable的&LT; SelectListItem&GT; DOCNAME =新的List&LT; SelectListItem&GT;();
DOCNAME =(从db.ServiceAccountFiltersŤ
其中,t.FilterId == ID
选择新的{t.DocNameId,t.FilterId})是不同的()选择(X =方式&gt;新SelectListItem()。
{
文= x.DocNameId,
值= x.DocNameId.ToString(),
组=新SelectListGroup(){名称= x.DocNameId.ToString()}
})鲜明()的OrderBy(X =方式&gt;。x.Text).ToList();
返回DOCNAME;
}
}#endregion
在你的控制器编辑动作
公众的ActionResult编辑(INT?ID)
{
如果(ID == NULL)
{
VAR SAF =新FilterModel();
返回查看(SAF);
}
VAR serviceAccountFilter = db.ServiceAccountFilters.Find(ID)
如果(serviceAccountFilter == NULL)
{
返回HttpNotFound();
} VAR模型=新FilterViewModel
{
FilterId = serviceAccountFilter.FilterId,
FILTERNAME = serviceAccountFilter.FilterName,
ServiceAccount = serviceAccountFilter.ServiceAccount,
的DocType = serviceAccountFilter.DocType,
DocTypeId = serviceAccountFilter.DocTypeId,
DocNameId = serviceAccountFilter.DocNameId,
LastModifiedById = serviceAccountFilter.LastModifiedById,
LastModifiedByName = serviceAccountFilter.LastModifiedByName,
上次更改时间= serviceAccountFilter.LastModified,
MonthsToReturn = serviceAccountFilter.MonthsToReturn,
ServiceAccountList = ServiceAccountList(),
DocNamesByDocTypeIdList = DocNamesByDocTypeIdList()
}; 返回查看(模型);
}
和这里是一个视图模型可以说它的名字将是 FilterViewModel
公共类FilterModel
{
[显示名称(过滤器ID)]
公众诠释FilterId {搞定;组; }
[显示名称(过滤器名称)]
公共字符串FILTERNAME {搞定;组; }
[显示名称(服务帐户)]
公共字符串ServiceAccount {搞定;组; }
[DisplayName的(文件类型)
公共字符串的DocType {搞定;组; }
[显示名称(文档类型ID)]
公众可空&LT; INT&GT; DocTypeId {搞定;组; }
[DisplayName的(文件名ID)]
公共字符串DocNameId {搞定;组; }
[显示名称(最后修改者ID)]
公共字符串LastModifiedById {搞定;组; }
[显示名称(最后修改者)]
公共字符串LastModifiedByName {搞定;组; }
[显示名称(最后修改)]
公众可空&LT;&System.DateTime的GT;上次更改{搞定;组; }
[显示名称(月回归)]
公众可空&LT; INT&GT; MonthsToReturn {搞定;组; } 公共IEnumerable的&LT; SelectListItem&GT; ServiceAccountList {搞定;组; }
公共IEnumerable的&LT; SelectListItem&GT; DocNamesByDocTypeIdList {搞定;组; }
}
和那么你的编辑视图会
@model FilterViewModel //检查名称空间@ {
ViewBag.Title =编辑过滤器
布局=〜/查看/共享/ _Layout.cshtml
}&LT; DIV CLASS =PAGETITLE过滤器过滤维护和LT; / DIV&GT;
@using(Html.BeginForm(新{ID =filterForm}))
{
@ Html.AntiForgeryToken() &LT; DIV CLASS =形横&GT;
&LT;小时/&GT;
@ Html.ValidationSummary(真,新{@class =TEXT-危险})
@ Html.HiddenFor(型号=&GT; model.FilterId)
@ Html.HiddenFor(型号=&GT; model.LastModified)
@ Html.HiddenFor(型号=&GT; model.LastModifiedById)
@ Html.HiddenFor(型号=&GT; model.LastModifiedByName) &LT; DIV CLASS =ddlGroups,btmMarg-15&GT;
@ Html.DropDownListFor(M = GT; m.ServiceAccount,Model.ServiceAccountList(),选择服务帐户)
&LT; / DIV&GT;
&LT; DIV CLASS =ddlGroups,COL-MD-10,btmMarg-15&GT;
@ Html.LabelFor(型号=&GT; model.ServiceAccount,htmlAttributes:新{@class =控制标签COL-MD-2})
@ Html.EditorFor(型号=&GT; model.ServiceAccount,新{htmlAttributes = {新@class =表格控}})
&LT; / DIV&GT;
&LT; DIV CLASS =ddlGroups,COL-MD-10,btmMarg-15&GT;
@ Html.LabelFor(型号=&GT; model.DocTypeId,htmlAttributes:新{@class =控制标签COL-MD-2})
@ Html.EditorFor(型号=&GT; model.DocTypeId,新{htmlAttributes = {新@class =表格控}})
&LT; / DIV&GT;
&下,P类=leftMarg-15文本默认ID =docNamesHdrText&GT;选择文档名称被包括在滤波器下; / P&GT;
&LT; DIV CLASS =ckDocNames&GT;
@foreach(在Model.DocNamesByDocTypeIdList变种DN())
{
&LT; DIV CLASS =checkboxContainer&GT;
&LT;输入类=ckBoxes类型=复选框NAME =DocNameIdVALUE =@ dn.ValueDTID =@ dn.Group.Name/>@dn.Text<br /&GT;
&LT; / DIV&GT;
}
&LT; / DIV&GT;
&LT; DIV CLASS =表单组,COL-MD-10,btmMarg-15ID =monthsGroup&GT;
@ Html.LabelFor(型号=&GT; model.MonthsToReturn,htmlAttributes:新{@class =控制标签COL-MD-2})
@ Html.EditorFor(型号=&GT; model.MonthsToReturn,新{htmlAttributes = {新@class =表格控}})
@ Html.ValidationMessageFor(型号=&GT; model.MonthsToReturn,新{@class =TEXT-危险})
&LT; / DIV&GT;
&LT; DIV CLASS =表单组,COL-MD-10,btmMarg-15ID =filterNameGroup&GT;
@ Html.LabelFor(型号=&GT; model.FilterName,htmlAttributes:新{@class =控制标签COL-MD-2})
@ Html.EditorFor(型号=&GT; model.FilterName,新{htmlAttributes = {新@class =表格控}})
@ Html.ValidationMessageFor(型号=&GT; model.FilterName,新{@class =TEXT-危险})
&LT; / DIV&GT;
&LT; BR /&GT;
&LT; DIV CLASS =表单组&GT;
&LT; DIV CLASS =COL-MD-偏移2 COL-MD-10&GT;
&LT;输入类型=提交值=保存级=BTN BTN-默认ID =modSaveButton/&GT;
&LT; / DIV&GT;
&LT; / DIV&GT;
&LT; / DIV&GT;
}&LT; DIV&GT;
@ Html.ActionLink(返回列表,ActiveFilters)|
@ Html.ActionLink(管理主页,指数)
&LT; / DIV&GT;@section脚本{
@ Scripts.Render(〜/包/ jqueryval)
}
I am writing a solution using MVC with scaffolding. It uses Entity Framework v6 and is database first in design. The base data model (ServiceAccountFilter) is extended by the data model (FilterModel). There is some logic in the FilterModel but the properties are all inherited from the base model. It seems as though the controller insists on using the base model. When I change the return to use the FilterModel, I get an error that the view requires the base model and then the functions in the FilterModel are not visible in the views? Not sure how to handle this?
Base Model:
namespace FHN.ARX.AdminWeb
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
public partial class ServiceAccountFilter
{
[DisplayName("Filter ID")]
public virtual int FilterId { get; set; }
[DisplayName("Filter Name")]
public virtual string FilterName { get; set; }
[DisplayName("Service Account")]
public virtual string ServiceAccount { get; set; }
[DisplayName("Doc Type")]
public virtual string DocType { get; set; }
[DisplayName("Doc Type ID")]
public virtual Nullable<int> DocTypeId { get; set; }
[DisplayName("Doc Name ID")]
public virtual string DocNameId { get; set; }
[DisplayName("Last Modified By ID")]
public virtual string LastModifiedById { get; set; }
[DisplayName("Last Modified By")]
public virtual string LastModifiedByName { get; set; }
[DisplayName("Last Modified")]
public virtual Nullable<System.DateTime> LastModified { get; set; }
[DisplayName("Months To Return")]
public virtual Nullable<int> MonthsToReturn { get; set; }
}
}
FilterModel
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace FHN.ARX.AdminWeb.Models
{
public class FilterModel : ServiceAccountFilter
{
[DisplayName("Filter ID")]
public override int FilterId { get; set; }
[DisplayName("Filter Name")]
public override string FilterName { get; set; }
[DisplayName("Service Account")]
public override string ServiceAccount { get; set; }
[DisplayName("Doc Type")]
public override string DocType { get; set; }
[DisplayName("Doc Type ID")]
public override Nullable<int> DocTypeId { get; set; }
[DisplayName("Doc Name ID")]
public override string DocNameId { get; set; }
[DisplayName("Last Modified By ID")]
public override string LastModifiedById { get; set; }
[DisplayName("Last Modified By")]
public override string LastModifiedByName { get; set; }
[DisplayName("Last Modified")]
public override Nullable<System.DateTime> LastModified { get; set; }
[DisplayName("Months To Return")]
public override Nullable<int> MonthsToReturn { get; set; }
public bool Selected { get; set; }
public string Checkboxes { get; set; }
public IEnumerable<SelectListItem> DocTypesList(string id)
{
using (var db = new ARXEntities())
{
var docType = new List<SelectListItem>();
docType = (from t in db.vwMapDocNamesToSecurityUsers
where t.UserDN == id
orderby t.DocType
select new { t.DocType, t.DocTypeId }).Distinct().Select(x => new SelectListItem() { Text = x.DocType, Value = x.DocTypeId.ToString() }).OrderBy(x => x.Text).ToList();
return docType;
}
}
public IEnumerable<SelectListItem> DocNamesList()
{
using (var db = new ARXEntities())
{
IEnumerable<SelectListItem> docName = new List<SelectListItem>();
var loggedInUser = "C2693"; // HttpContext.Current.User.Identity.Name.Split('\\')[1];
docName = (from t in db.vwMapDocNamesToSecurityUsers
where t.UserDN == loggedInUser
select new { t.DocName, t.DocNameId, t.DocTypeId }).Distinct().Select(x => new SelectListItem()
{
Text = x.DocName,
Value = x.DocNameId.ToString(),
Group = new SelectListGroup() { Name = x.DocTypeId.ToString() }
}).Distinct().OrderBy(x => x.Text).ToList();
var docCount = docName.Count();
return docName;
}
}
public IEnumerable<SelectListItem> ServiceAccountList()
{
using (var db = new ARXEntities())
{
var sa = new List<SelectListItem>();
sa = (from t in db.vwMapDocNamesToSecurityUsers
where t.UserDN.StartsWith("sa_")
orderby t.UserDN
select new { t.UserDN }).Distinct().Select(x => new SelectListItem() { Text = x.UserDN, Value = x.UserDN }).OrderBy(x => x.Text).ToList();
return sa;
}
}
public IEnumerable<SelectListItem> DocNamesByDocTypeIdList()
{
using (var db = new ARXEntities())
{
IEnumerable<SelectListItem> docName = new List<SelectListItem>();
docName = (from t in db.vwMapDocNamesToSecurityUsers
select new { t.DocName, t.DocNameId, t.DocTypeId }).Distinct().Select(x => new SelectListItem()
{
Text = x.DocName,
Value = x.DocNameId.ToString(),
Group = new SelectListGroup() { Name = x.DocTypeId.ToString() }
}).Distinct().OrderBy(x => x.Text).ToList();
var docCount = docName.Count();
return docName;
}
}
public IEnumerable<SelectListItem> GetDocNamesForFilterId(int? id)
{
using (var db = new ARXEntities())
{
IEnumerable<SelectListItem> docName = new List<SelectListItem>();
docName = (from t in db.ServiceAccountFilters
where t.FilterId == id
select new { t.DocNameId, t.FilterId }).Distinct().Select(x => new SelectListItem()
{
Text = x.DocNameId,
Value = x.DocNameId.ToString(),
Group = new SelectListGroup() { Name = x.DocNameId.ToString() }
}).Distinct().OrderBy(x => x.Text).ToList();
return docName;
}
}
}
}
Controller Edit Action
public ActionResult Edit(int? id)
{
if (id == null)
{
var saf = new FilterModel();
return View(saf);
}
FilterModel serviceAccountFilter = (FilterModel)db.ServiceAccountFilters.Find(id); <----Tried casting here, but still didnt work.
if (serviceAccountFilter == null)
{
return HttpNotFound();
}
return View(serviceAccountFilter);
}
Edit View
@model FHN.ARX.AdminWeb.Models.FilterModel
@{
ViewBag.Title = "Edit A Filter";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="pageTitle">Filter Maintenance</div>
@using (Html.BeginForm(new { id = "filterForm" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.FilterId)
@Html.HiddenFor(model => model.LastModified)
@Html.HiddenFor(model => model.LastModifiedById)
@Html.HiddenFor(model => model.LastModifiedByName)
<div class="ddlGroups, btmMarg-15">
@Html.DropDownListFor(m => m.ServiceAccount, Model.ServiceAccountList(), "Select a Service Account")
</div>
<div class="ddlGroups, col-md-10, btmMarg-15">
@Html.LabelFor(model => model.ServiceAccount, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.ServiceAccount, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="ddlGroups, col-md-10, btmMarg-15">
@Html.LabelFor(model => model.DocTypeId, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.DocTypeId, new { htmlAttributes = new { @class = "form-control" } })
</div>
<p class="leftMarg-15 text-default" id="docNamesHdrText">Select the document names to be included in the filter.</p>
<div class="ckDocNames">
@foreach (var dn in Model.DocNamesByDocTypeIdList())
{
<div class="checkboxContainer">
<input class="ckBoxes" type="checkbox" name="DocNameId" value="@dn.Value" dtid="@dn.Group.Name" />@dn.Text<br />
</div>
}
</div>
<div class="form-group, col-md-10, btmMarg-15" id="monthsGroup">
@Html.LabelFor(model => model.MonthsToReturn, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.MonthsToReturn, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MonthsToReturn, "", new { @class = "text-danger" })
</div>
<div class="form-group, col-md-10, btmMarg-15" id="filterNameGroup">
@Html.LabelFor(model => model.FilterName, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.FilterName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FilterName, "", new { @class = "text-danger" })
</div>
<br />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" id="modSaveButton" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "ActiveFilters") |
@Html.ActionLink("Admin Home", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
this is an example of using a viewmodel on your case ... (may be it's not prefect but just a hint on your way)
namespace FHN.ARX.AdminWeb
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
public partial class ServiceAccountFilter
{
public int FilterId { get; set; }
public string FilterName { get; set; }
public string ServiceAccount { get; set; }
public string DocType { get; set; }
public Nullable<int> DocTypeId { get; set; }
public string DocNameId { get; set; }
public string LastModifiedById { get; set; }
public string LastModifiedByName { get; set; }
public Nullable<System.DateTime> LastModified { get; set; }
public Nullable<int> MonthsToReturn { get; set; }
}
}
no need for the filter model now and instead of that put all the logic in the controller under a heper region
#region Helpers
public IEnumerable<SelectListItem> DocTypesList(string id)
{
using (var db = new ARXEntities())
{
var docType = new List<SelectListItem>();
docType = (from t in db.vwMapDocNamesToSecurityUsers
where t.UserDN == id
orderby t.DocType
select new { t.DocType, t.DocTypeId }).Distinct().Select(x => new SelectListItem() { Text = x.DocType, Value = x.DocTypeId.ToString() }).OrderBy(x => x.Text).ToList();
return docType;
}
}
public IEnumerable<SelectListItem> DocNamesList()
{
using (var db = new ARXEntities())
{
IEnumerable<SelectListItem> docName = new List<SelectListItem>();
var loggedInUser = "C2693"; // HttpContext.Current.User.Identity.Name.Split('\\')[1];
docName = (from t in db.vwMapDocNamesToSecurityUsers
where t.UserDN == loggedInUser
select new { t.DocName, t.DocNameId, t.DocTypeId }).Distinct().Select(x => new SelectListItem()
{
Text = x.DocName,
Value = x.DocNameId.ToString(),
Group = new SelectListGroup() { Name = x.DocTypeId.ToString() }
}).Distinct().OrderBy(x => x.Text).ToList();
var docCount = docName.Count();
return docName;
}
}
public IEnumerable<SelectListItem> ServiceAccountList()
{
using (var db = new ARXEntities())
{
var sa = new List<SelectListItem>();
sa = (from t in db.vwMapDocNamesToSecurityUsers
where t.UserDN.StartsWith("sa_")
orderby t.UserDN
select new { t.UserDN }).Distinct().Select(x => new SelectListItem() { Text = x.UserDN, Value = x.UserDN }).OrderBy(x => x.Text).ToList();
return sa;
}
}
public IEnumerable<SelectListItem> DocNamesByDocTypeIdList()
{
using (var db = new ARXEntities())
{
IEnumerable<SelectListItem> docName = new List<SelectListItem>();
docName = (from t in db.vwMapDocNamesToSecurityUsers
select new { t.DocName, t.DocNameId, t.DocTypeId }).Distinct().Select(x => new SelectListItem()
{
Text = x.DocName,
Value = x.DocNameId.ToString(),
Group = new SelectListGroup() { Name = x.DocTypeId.ToString() }
}).Distinct().OrderBy(x => x.Text).ToList();
var docCount = docName.Count();
return docName;
}
}
public IEnumerable<SelectListItem> GetDocNamesForFilterId(int? id)
{
using (var db = new ARXEntities())
{
IEnumerable<SelectListItem> docName = new List<SelectListItem>();
docName = (from t in db.ServiceAccountFilters
where t.FilterId == id
select new { t.DocNameId, t.FilterId }).Distinct().Select(x => new SelectListItem()
{
Text = x.DocNameId,
Value = x.DocNameId.ToString(),
Group = new SelectListGroup() { Name = x.DocNameId.ToString() }
}).Distinct().OrderBy(x => x.Text).ToList();
return docName;
}
}
#endregion
in your controller Edit Action
public ActionResult Edit(int? id)
{
if (id == null)
{
var saf = new FilterModel();
return View(saf);
}
var serviceAccountFilter = db.ServiceAccountFilters.Find(id)
if (serviceAccountFilter == null)
{
return HttpNotFound();
}
var model = new FilterViewModel
{
FilterId = serviceAccountFilter.FilterId,
FilterName = serviceAccountFilter.FilterName,
ServiceAccount = serviceAccountFilter.ServiceAccount,
DocType = serviceAccountFilter.DocType,
DocTypeId = serviceAccountFilter.DocTypeId,
DocNameId = serviceAccountFilter.DocNameId,
LastModifiedById = serviceAccountFilter.LastModifiedById,
LastModifiedByName = serviceAccountFilter.LastModifiedByName,
LastModified = serviceAccountFilter.LastModified,
MonthsToReturn = serviceAccountFilter.MonthsToReturn,
ServiceAccountList = ServiceAccountList(),
DocNamesByDocTypeIdList = DocNamesByDocTypeIdList()
};
return View(model);
}
and here is a ViewModel lets say it's name will be FilterViewModel
public class FilterModel
{
[DisplayName("Filter ID")]
public int FilterId { get; set; }
[DisplayName("Filter Name")]
public string FilterName { get; set; }
[DisplayName("Service Account")]
public string ServiceAccount { get; set; }
[DisplayName("Doc Type")]
public string DocType { get; set; }
[DisplayName("Doc Type ID")]
public Nullable<int> DocTypeId { get; set; }
[DisplayName("Doc Name ID")]
public string DocNameId { get; set; }
[DisplayName("Last Modified By ID")]
public string LastModifiedById { get; set; }
[DisplayName("Last Modified By")]
public string LastModifiedByName { get; set; }
[DisplayName("Last Modified")]
public Nullable<System.DateTime> LastModified { get; set; }
[DisplayName("Months To Return")]
public Nullable<int> MonthsToReturn { get; set; }
public IEnumerable<SelectListItem> ServiceAccountList { get; set; }
public IEnumerable<SelectListItem> DocNamesByDocTypeIdList { get; set; }
}
and then your Edit View will be
@model FilterViewModel // check the name space
@{
ViewBag.Title = "Edit A Filter";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="pageTitle">Filter Maintenance</div>
@using (Html.BeginForm(new { id = "filterForm" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.FilterId)
@Html.HiddenFor(model => model.LastModified)
@Html.HiddenFor(model => model.LastModifiedById)
@Html.HiddenFor(model => model.LastModifiedByName)
<div class="ddlGroups, btmMarg-15">
@Html.DropDownListFor(m => m.ServiceAccount, Model.ServiceAccountList(), "Select a Service Account")
</div>
<div class="ddlGroups, col-md-10, btmMarg-15">
@Html.LabelFor(model => model.ServiceAccount, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.ServiceAccount, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="ddlGroups, col-md-10, btmMarg-15">
@Html.LabelFor(model => model.DocTypeId, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.DocTypeId, new { htmlAttributes = new { @class = "form-control" } })
</div>
<p class="leftMarg-15 text-default" id="docNamesHdrText">Select the document names to be included in the filter.</p>
<div class="ckDocNames">
@foreach (var dn in Model.DocNamesByDocTypeIdList())
{
<div class="checkboxContainer">
<input class="ckBoxes" type="checkbox" name="DocNameId" value="@dn.Value" dtid="@dn.Group.Name" />@dn.Text<br />
</div>
}
</div>
<div class="form-group, col-md-10, btmMarg-15" id="monthsGroup">
@Html.LabelFor(model => model.MonthsToReturn, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.MonthsToReturn, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MonthsToReturn, "", new { @class = "text-danger" })
</div>
<div class="form-group, col-md-10, btmMarg-15" id="filterNameGroup">
@Html.LabelFor(model => model.FilterName, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.FilterName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FilterName, "", new { @class = "text-danger" })
</div>
<br />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" id="modSaveButton" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "ActiveFilters") |
@Html.ActionLink("Admin Home", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
这篇关于问题延伸基地EDMX模型和调用功能,在数据模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!