简体   繁体   English

将数据从视图模型添加到模型

[英]Add data from viewmodel to models

So here is my controller.所以这是我的 controller。 The ViewModel contains a member's PIN and an Events ID. ViewModel 包含成员的 PIN 和事件 ID。 Now I want to find which event is associated with the Id, and which member is associated with the Pin.现在我想找出哪个事件与 Id 相关联,以及哪个成员与 Pin 相关联。 Once I have that information, I want to associate this event to the members whose PIN was entered.获得该信息后,我想将此事件与输入了 PIN 的成员相关联。 I'll add the member class and event class below.我将在下面添加成员 class 和事件 class。 (it is a many to many relationships) I'm just confused on how to add the event to the member table (这是多对多的关系)我只是对如何将事件添加到成员表感到困惑

[HttpPost]
public ActionResult SignUp(MemberEventViewModel Vmodel) //signup action
{
var MemberinDB = _context.Members.Where(c => c.PIN == Vmodel.Member.PIN);

var EventinDB = _context.Events.Where(c => c.Id == Vmodel.Events.Id);
_context.Members.Add(EventinDB);
_context.SaveChanges();
}

member class成员 class

public class Member
{
    public int Id { get; set; }
    [Required]
    [MaxLength(4, ErrorMessage = "PIN must be 4 numbers long"), MinLength(4, ErrorMessage = "PIN must be 4 numbers long")]
    public string PIN { get; set; }
    [Required]
    [Display(Name ="First Name")]
    public string FirstName { get; set; }
    [Required]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }
    [Display(Name = "Date of Birth")]
    public DateTime? Birthdate { get; set; }
    public virtual ICollection<Event> Events { get; set; }

}

event class事件 class

public class Event
{

    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    [Display(Name = "Date")]
    public DateTime? EventDate { get; set; }
    [Required]
    [Display(Name = "Start Time")]
    public TimeSpan EventStartTime { get; set; }
    [Required]
    [Display(Name = "End Time")]
    public TimeSpan EventEndTime { get; set; }
    public int EventTotalTime{ get; set; }
    public virtual ICollection<Member> Members { get; set; }

}

Use the navigation properties to associate your new relationship.使用导航属性关联您的新关系。

var MemberinDB = _context.Members.Include(c => c.Events).Single(c => c.PIN == Vmodel.Member.PIN);

if(MemberinDB.Events.Any(e => x.Id == VModel.Event.Id))
    return; // Member and event already associated.

var EventinDB = _context.Events.Single(c => c.Id == Vmodel.Event.Id);
MemberInDB.Events.Add(EventinDB);

_context.SaveChanges();

This loads the member and includes the events associated.这将加载成员并包括关联的事件。 Your member and events relationship is many to many, so you could do this from the member to add the event or from the event to add the member.您的成员和事件关系是多对多的,因此您可以从成员添加事件或从事件添加成员。 The above example checks that the event hadn't been added, then retrieves it and adds it to the member if not.上面的示例检查该事件是否未被添加,然后检索它并将其添加到成员中,如果没有。

Your view model implies that a member has Events, rather than passing a view model to represent just a new member-event association.您的视图 model 暗示一个成员有事件,而不是传递一个视图 model 来表示一个新的成员事件关联。 If you are passing a view model that contains a member and all of their events (including existing associations and new ones) then you may need some additional logic to identify which event associations may have been added or removed to handle those.如果您传递的视图 model 包含一个成员及其所有事件(包括现有关联和新关联),那么您可能需要一些额外的逻辑来识别可能已添加或删除哪些事件关联来处理这些事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM