将视图模型传递给视图,将文本框中的数据保存到视图模型中,然后将该数据从视图模型传输到我的模型
Passing a view-model to view, saving data from text-boxes into the view-model, then transferring that data from the view-model to my models
我不知道我这样做的方式是否正确,但基本上我正在为一个俱乐部创建一个 MVC 应用程序,我有一个会员 class 和一个活动 class .我有一个显示以下所有事件的视图
@model IEnumerable<Application.Models.Event>
@{
Layout = null;
}
<h1>Events For this Month!</h1>
<table>
@foreach (var events in Model)
{
<tr>
<td><h2>@events.Name</h2></td>
<td>
@Html.ActionLink("Sign-Up", "SignUpEvent", "User", new { id = events.Id }, null)
</td>
</tr>
}
</table>
当他们 select 在他们希望参加的活动旁边注册时,他们会转到此操作。我创建了一个名为“MemberEventViewModel”的视图模型,其中包含一个成员和一个事件。
public ActionResult SignUpEvent(int id)
{
var Vmodel = new MemberEventViewModel()
{
};
return View(Vmodel);
}
返回的视图是会员输入 PIN 码的地方
@model Application.ViewModels.MemberEventViewModel
@{
Layout = null;
}
<h2>Sign-Up</h2>
@using (Html.BeginForm("SignUp", "User"))
{
@Html.LabelFor(c => c.Member.PIN)
@Html.TextBoxFor(c => c.Member.PIN)
@Html.HiddenFor(c => c.Events.Id)
<button type="submit">Save</button>
}
然后最终转到此控制器,这样我就可以保存并查看会员已注册的活动
[HttpPost]
public ActionResult SignUp(MemberEventViewModel Vmodel)
{
var EventinDB = _context.Events.SingleOrDefault(c => c.Id == Vmodel.Events.Id);
var MemberinDB = _context.Members.SingleOrDefault(c => c.PIN == Vmodel.Member.PIN);
MemberinDB.Events.Add(EventinDB);
EventinDB.Members.Add(MemberinDB);
return View("ViewEvents", "User");
我对如何让它工作感到困惑。我想查看会员注册了哪些活动,反之亦然。当我在事件和成员之间创建多对多关系时,创建了一个名为 MemberEvents 的桥 table。
这也是我的视图模型class
public class MemberEventViewModel
{
public Member Member { get; set; }
public Event Events { get; set; }
}
这是事件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; }
}
和成员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; }
}
看起来您的添加方式没有任何问题,但我认为您在添加 MemberEvents 对象后错过了 context.SaveChanges()
。
[HttpPost]
public ActionResult SignUp(MemberEventViewModel Vmodel)
{
var EventinDB = _context.Events.SingleOrDefault(c => c.Id == Vmodel.Events.Id);
var MemberinDB = _context.Members.SingleOrDefault(c => c.PIN == Vmodel.Member.PIN);
// try using one only
MemberinDB.Events.Add(EventinDB);
// EventinDB.Members.Add(MemberinDB);
// you should save here
_context.SaveChanges();
return View("ViewEvents", "User");
}
还要确保您在此处分配属性;
public ActionResult SignUpEvent(int id)
{
var Vmodel = new MemberEventViewModel()
{
Member = // find the member _context.Members.Where(...)
Events = _context.Events.FirstOrDefault(e=>e.Id == id)
};
return View(Vmodel);
}
我不知道我这样做的方式是否正确,但基本上我正在为一个俱乐部创建一个 MVC 应用程序,我有一个会员 class 和一个活动 class .我有一个显示以下所有事件的视图
@model IEnumerable<Application.Models.Event>
@{
Layout = null;
}
<h1>Events For this Month!</h1>
<table>
@foreach (var events in Model)
{
<tr>
<td><h2>@events.Name</h2></td>
<td>
@Html.ActionLink("Sign-Up", "SignUpEvent", "User", new { id = events.Id }, null)
</td>
</tr>
}
</table>
当他们 select 在他们希望参加的活动旁边注册时,他们会转到此操作。我创建了一个名为“MemberEventViewModel”的视图模型,其中包含一个成员和一个事件。
public ActionResult SignUpEvent(int id)
{
var Vmodel = new MemberEventViewModel()
{
};
return View(Vmodel);
}
返回的视图是会员输入 PIN 码的地方
@model Application.ViewModels.MemberEventViewModel
@{
Layout = null;
}
<h2>Sign-Up</h2>
@using (Html.BeginForm("SignUp", "User"))
{
@Html.LabelFor(c => c.Member.PIN)
@Html.TextBoxFor(c => c.Member.PIN)
@Html.HiddenFor(c => c.Events.Id)
<button type="submit">Save</button>
}
然后最终转到此控制器,这样我就可以保存并查看会员已注册的活动
[HttpPost]
public ActionResult SignUp(MemberEventViewModel Vmodel)
{
var EventinDB = _context.Events.SingleOrDefault(c => c.Id == Vmodel.Events.Id);
var MemberinDB = _context.Members.SingleOrDefault(c => c.PIN == Vmodel.Member.PIN);
MemberinDB.Events.Add(EventinDB);
EventinDB.Members.Add(MemberinDB);
return View("ViewEvents", "User");
我对如何让它工作感到困惑。我想查看会员注册了哪些活动,反之亦然。当我在事件和成员之间创建多对多关系时,创建了一个名为 MemberEvents 的桥 table。
这也是我的视图模型class
public class MemberEventViewModel
{
public Member Member { get; set; }
public Event Events { get; set; }
}
这是事件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; }
}
和成员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; }
}
看起来您的添加方式没有任何问题,但我认为您在添加 MemberEvents 对象后错过了 context.SaveChanges()
。
[HttpPost]
public ActionResult SignUp(MemberEventViewModel Vmodel)
{
var EventinDB = _context.Events.SingleOrDefault(c => c.Id == Vmodel.Events.Id);
var MemberinDB = _context.Members.SingleOrDefault(c => c.PIN == Vmodel.Member.PIN);
// try using one only
MemberinDB.Events.Add(EventinDB);
// EventinDB.Members.Add(MemberinDB);
// you should save here
_context.SaveChanges();
return View("ViewEvents", "User");
}
还要确保您在此处分配属性;
public ActionResult SignUpEvent(int id)
{
var Vmodel = new MemberEventViewModel()
{
Member = // find the member _context.Members.Where(...)
Events = _context.Events.FirstOrDefault(e=>e.Id == id)
};
return View(Vmodel);
}