问题描述
这可能很难解释,但请耐心等待......所以,我有一个结合了两种不同模型的模型,以便我可以在一个视图中使用两种数据:组合:
This might be difficult to explain but please bear with me... So, I have a model that combines two different models so that I can utilize data found in both in one view: Combined:
public class EventFullModel
{
public AccessListModel AccessPointsList;
public EventListModel EventList;
}
访问列表型号:
Access List Model:
public class AccessListModel
{
public int Id { get; set; }
public List<SelectListItem> AccessPoints { get; set; }
}
活动清单型号:
Event List Model:
public class EventListModel
{
public int Id { get; set; }
public virtual string T { get; set; }
public virtual string C { get; set; }
public virtual string M { get; set; }
public virtual string L { get; set; }
public List<SelectListItem> Events { get; set; }
public IEnumerable<SelectListItem> Skills { get; set; }
}
我目前收到一个错误,我认为这个错误源于此视图的控制器方法最初只返回EventListModel,而现在它正在尝试返回EventFullModel,因为我更改了继承:Inherits =System.Web.Mvc.ViewPage< cred.web.models.eventfullmodel>。所以此时我正在尝试编辑控制器以确保它尝试为视图返回正确的模型。我的问题如下:
这是一个GET操作方法,只接受(int?id)作为参数。
最初,控制器只能使用EventListModel:
I am currently getting an error which I believe stems from the fact that the controller method for this view originally returned just the EventListModel whereas now it is trying to return the EventFullModel since I changed the inheritance: Inherits="System.Web.Mvc.ViewPage<cred.web.models.eventfullmodel>". So at this point I am trying to edit the controller to make sure it attempts to return the correct model for the view. My problem is the following:
This is a GET action method that only accepts (int? id) as a parameter.
Originally, the controller worked only with the EventListModel:
EventListModel model;
model = new EventListModel {
Id = 0,
T = "",
C = "",
M = "",
L = HttpContext.Session["L"].IsNull() ? "" : HttpContext.Session["L"].ToString(),
Events = new List<SelectListItem>(),
Skills =
DbQueryExecutor.ExecuteQuery(new SkillsList()).Select(e => new SelectListItem { Text = e.Certification, Value = e.Certification, Selected = false })
所以我基本上想知道,如何编辑它来访问EventListModel的那些部分但是我的模型= new EventFullModel ?
(免责声明:我已经改变了一些东西的名称,我正在编辑一个我最初没写的软件,所以我试图为了不破坏软件的其他部分,希望尽可能少地改变。
So I am basically wondering, how can I edit this to access those pieces of the EventListModel but have my model = new EventFullModel?
(Disclaimer: I have altered the names of a few things and I am working to edit a piece of software that I did not originally write so I am trying to change as little as possible in the hopes of not "breaking" other pieces of the software)
推荐答案
EventListModel modelEvent;
AccessListModel modelAccess;
modelEvent = new EventListModel {
Id = 0,
...}
modelAccess = new AccessListModel {
Id = 0,
...}
var modelFull = new EventFullModel {
EventsList = modelEvent,
AccessPointsList = modelAccess };
return View(modelFull);
这篇关于访问控制器中的组合模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!