Asp.net mvc 问题扩展基本EDMX模型并调用数据模型中的函数

Asp.net mvc 问题扩展基本EDMX模型并调用数据模型中的函数,asp.net-mvc,asp.net-mvc-4,entity-framework-6,Asp.net Mvc,Asp.net Mvc 4,Entity Framework 6,我正在编写一个使用MVC和脚手架的解决方案。它使用EntityFrameworkV6,是设计中的第一个数据库。基本数据模型(ServiceAccountFilter)由数据模型(FilterModel)扩展。FilterModel中有一些逻辑,但属性都是从基本模型继承的。控制器似乎坚持使用基本模型。当我将返回更改为使用FilterModel时,我得到一个错误,即视图需要基本模型,然后FilterModel中的函数在视图中不可见?不知道怎么处理 基本型号: namespace FHN.ARX.A

我正在编写一个使用MVC和脚手架的解决方案。它使用EntityFrameworkV6,是设计中的第一个数据库。基本数据模型(ServiceAccountFilter)由数据模型(FilterModel)扩展。FilterModel中有一些逻辑,但属性都是从基本模型继承的。控制器似乎坚持使用基本模型。当我将返回更改为使用FilterModel时,我得到一个错误,即视图需要基本模型,然后FilterModel中的函数在视图中不可见?不知道怎么处理

基本型号:

 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; }
        }
    }
名称空间FHN.ARX.AdminWeb
{
使用制度;
使用System.Collections.Generic;
使用系统组件模型;
公共部分类ServiceAccountFilter
{
[显示名称(“过滤器ID”)]
公共虚拟int FilterId{get;set;}
[显示名称(“过滤器名称”)]
公共虚拟字符串筛选器名称{get;set;}
[显示名称(“服务帐户”)]
公共虚拟字符串ServiceAccount{get;set;}
[显示名称(“文档类型”)]
公共虚拟字符串DocType{get;set;}
[显示名称(“文档类型ID”)]
公共虚拟可空DocTypeId{get;set;}
[显示名称(“文件名称ID”)]
公共虚拟字符串DocNameId{get;set;}
[显示名称(“上次由ID修改”)]
公共虚拟字符串LastModifiedById{get;set;}
[显示名称(“上次修改人”)]
公共虚拟字符串LastModifiedByName{get;set;}
[显示名称(“上次修改”)]
公共虚拟可空LastModified{get;set;}
[显示名称(“返回月份”)]
公共虚拟可为空的MonthStoreReturn{get;set;}
}
}
过滤器模型

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;
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
命名空间FHN.ARX.AdminWeb.Models
{
公共类筛选器模型:ServiceAccountFilter
{
[显示名称(“过滤器ID”)]
公共重写int FilterId{get;set;}
[显示名称(“过滤器名称”)]
公共重写字符串筛选器名称{get;set;}
[显示名称(“服务帐户”)]
公共重写字符串ServiceAccount{get;set;}
[显示名称(“文档类型”)]
公共重写字符串DocType{get;set;}
[显示名称(“文档类型ID”)]
公共重写可为空的DocTypeId{get;set;}
[显示名称(“文件名称ID”)]
公共重写字符串DocNameId{get;set;}
[显示名称(“上次由ID修改”)]
公共重写字符串LastModifiedById{get;set;}
[显示名称(“上次修改人”)]
公共重写字符串LastModifiedByName{get;set;}
[显示名称(“上次修改”)]
公共重写可为Null的LastModified{get;set;}
[显示名称(“返回月份”)]
公共重写可为空的MonthStoreReturn{get;set;}
已选择公共布尔值{get;set;}
公共字符串复选框{get;set;}
公共IEnumerable DocTypesList(字符串id)
{
使用(var db=new arxenties())
{
var docType=new List();
docType=(从db.vwMapDocNamesToSecurityUsers中的t开始)
其中t.UserDN==id
orderby t.DocType
选择new{t.DocType,t.DocTypeId}).Distinct().select(x=>new-SelectListItem(){Text=x.DocType,Value=x.DocTypeId.ToString()).OrderBy(x=>x.Text.ToList();
返回docType;
}
}
公共IEnumerable DocNamesList()
{
使用(var db=new arxenties())
{
IEnumerable docName=新列表();
var loggedInUser=“C2693”;//HttpContext.Current.User.Identity.Name.Split(“\\”)[1];
docName=(从db.vwmapDocNames中的t到安全用户
其中t.UserDN==loggedInUser
选择新建{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();
返回docName;
}
}
public IEnumerable ServiceAccountList()
{
使用(var db=new arxenties())
{
var sa=新列表();
sa=(从db.VWMapDocNames中的t到安全用户
其中t.UserDN.StartsWith(“sa_”)
orderby t.UserDN
选择new{t.UserDN}).Distinct().select(x=>newselectListItem(){Text=x.UserDN,Value=x.UserDN}).OrderBy(x=>x.Text).ToList();
返回sa;
}
}
public IEnumerable DocNamesByDocTypeIdList()
{
使用(var db=new arxenties())
{
IEnumerable docName=新列表();
docName=(从db.vwmapDocNames中的t到安全用户
选择新建{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
 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);
        }
@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")
}
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; }
    }
}
#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
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);
}
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; }
}
@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")
}