问题扩展了基础EDMX模型并调用数据模型中的函数

时间:2015-10-20 20:59:31

标签: asp.net-mvc asp.net-mvc-4 entity-framework-6

我正在使用带脚手架的MVC编写解决方案。它使用Entity Framework v6并且在设计中是数据库第一。基础数据模型(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; }
        }
    }

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;
            }
        }
    }
}

控制器编辑操作

 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")
}

1 个答案:

答案 0 :(得分:0)

这是在你的案例中使用视图模型的一个例子......(可能它不是完美的,但只是你的方式提示)

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

之下
#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);
}

这里是一个ViewModel,我们可以说它的名字是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; }
}

然后您的编辑视图将

@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")
}