使用视图模型:根据id分配对象

时间:2015-10-05 09:35:30

标签: c# entity-framework asp.net-mvc-5 viewmodel

我有以下模型类和视图模型。我需要为Option赋予OptionID和TechnicalCharacteristic的id为TCID的对象,即我需要从TechnicalCharacteristic中检索特定的行,其中id = TCID,从Option中检索id = OptionID的行。

技术特点

public class TechnicalCharacteristic
{
    public int TechnicalCharacteristicID { get; set; }
    [Display (Name = "Technical Characteristic Name")]
    public string TCName { get; set; }
    [Display (Name = "Description in English")]
    public string DescriptionEN { get; set; }
    [Display (Name = "Description in German")]
    public string DescriptionDE { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime ModifiedOn { get; set; }
    public string CreatedBy { get; set; }
    public string ModifiedBy { get; set; }

    public virtual ICollection<TcSet> TcSets { get; set; }
    //public virtual ICollection<Option> Options { get; set; }
}

public class Option
{
    public int OptionID { get; set;}
    [Display (Name = "Option Type")]
    //[Remote("DuplicateOptionName", "Options", HttpMethod = "POST", ErrorMessage = "Option Name already Exists", AdditionalFields = "")]
    public string OptionName { get; set; }
    [Display (Name ="Description in English")]
    public string DescriptionEN { get; set; }
    [Display(Name = "Description in German")]
    public string DescriptionDE { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime ModifiedOn { get; set; }
    public string CreatedBy { get; set; }
    public string ModifiedBy { get; set; }

    public int TechnicalCharacteristicID { get; set; }
    public int LsystemID { get; set; }

    public virtual ICollection<OptionValue> OptionValues { get; set; }
    public virtual TechnicalCharacteristic TechnicalCharacteristic { get; set; }
    public virtual Lsystem Lsystem { get; set; }
   // public virtual ICollection< SetValue> SetValue { get; set; }
}

选项值

public class OptionValue
{
    public int OptionValueID { get; set; }
    [Display(Name = "Option Value")]
    public string OptionVal { get; set; }
    [Display (Name="Created By")]
    public string CreatedBy { get; set; }
    [Display(Name = "Created On")]
    public DateTime CreatedOn { get; set; }
    [Display(Name = "Modified By")]
    public string ModifiedBy { get; set; }
    [Display(Name = "Modified On")]
    public DateTime ModifiedOn { get; set; }
    public int OptionID { get; set; }
   // public int SetValueID { get; set; }

    public virtual Option Option { get; set; }
    public virtual ICollection< SetValue> SetValue { get; set; }
}

TcSet

public class TcSet
{
    public int TcSetID { get; set; }
    [Display (Name = "Property name")]
    public string SetName { get; set; }
    [Display(Name = "PhysicalUnit")]
    public string PhysicalUnit { get; set; }
    [Display (Name = "Data Usage")]
    public DataUsage DataUsage { get; set; }
    [Display (Name = "Data Status")]
    public DataStatus DataStatus { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime ModifiedOn { get; set; }
    [Display (Name = "Description in German")]
    public string DescriptionDE { get; set; }
    [Display (Name = "Description in English")]
    public string DescriptionEN { get; set; }
    public string CreatedBy { get; set; }
    public string ModifiedBy { get; set; }

    public int TechnicalCharacteristicID { get; set; }
    public int DataFormatID { get; set; }

    public virtual ICollection<SetValue> SetValues { get; set; }
    public virtual DataFormat DataFormat { get; set; }
    public virtual TechnicalCharacteristic TechnicalCharacteristic { get; set; }
}

视图模型

public class AddOptionValue
{
    public virtual OptionValue OptionValue { get; set; }
    public virtual SetValue SetValue { get; set; }
    public virtual TechnicalCharacteristic TechnicalCharacteristic { get; set; }
    public virtual Option Option { get; set; }
    public virtual IEnumerable<TcSet> TcSets { get; set; }
}

控制器

    public ActionResult AddOptionValue (int OptionID, int TCID)
    {
        var viewModel = new AddOptionValue();
        var option = db.Option.Include(x=>x.OptionID).FirstOrDefault(x => x.OptionID == OptionID);
        var tc = db.TechnicalCharacteristic.Include(x => x.TCName).FirstOrDefault(x => x.TechnicalCharacteristicID == TCID);
        var TcSet = db.TechnicalCharacteristic.Include(x => x.TcSets).FirstOrDefault(x=>x.TechnicalCharacteristicID==TCID);
        if(option!=null&&TcSet!=null)
        {
            viewModel.Option = option;
            viewModel.TechnicalCharacteristic = tc;
            viewModel.TcSets = TcSet.TcSets;
        }
        return View(viewModel);
    }

查看

@model TEDALS_Ver01.ViewModels.AddOptionValue
@{
ViewBag.Title = "AddOptionValue";
}

<h2>Add Option Value</h2>

<table>
<tr>
    <th> Option Name</th>
    <th> Technical Characteristic</th>
    <th></th>
</tr>
<tr>
    <td>@Model.Option.OptionName</td>
    <td>@Model.TechnicalCharacteristic.TCName</td>
</tr>
@foreach (var item in Model.TcSets)
{
    <tr>

        <td>item.SetName</td>
    </tr>
}
</table>

我想要实现的目标

我获取tcSet TcSet.TechnicalCharacteristicID == TCID中所有元素列表的表。

我在代码中尝试了一些变体,但没有一个产生任何结果。

 An exception of type 'System.InvalidOperationException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
 Additional information: A specified Include path is not valid. The EntityType 'TEDALS_Ver01.DAL.Option' does not declare a navigation property with the name 'OptionID'.

是我在线上的错误

 var option = db.Option.Include(x=>x.OptionID).FirstOrDefault(x => x.OptionID == OptionID);    

我不确定ViewModel是否是实现相同目标的正确方法。除了列出TcSet之外,我还需要为在另一个模型类中完成的TcSet赋值。

1 个答案:

答案 0 :(得分:1)

根据您的评论,我认为您不需要Include()您只需要提供的ID值中的选项,您可以删除它,但如果您想要选项值,请执行以下操作:

var option = db.Option.Include(x => x.OptionValues).FirstOrDefault(x => x.OptionID == OptionID);

或没有OptionValues

var option = db.Option.FirstOrDefault(x => x.OptionID == OptionID);