![](/img/trans.png)
[英]CRUD operations for multiple related records in different tables from single view in MVC C#
[英]Problem saving new records to multiple tables from a single view
我正在嘗試將數據庫中的新記錄添加到多個相關表中,這些表的ID為PK,其他表的ID為FK。 我的方法基於NerdDinner教程,但是當涉及多個表時,找不到如何在數據庫中添加新記錄的任何示例。
由於某種原因嘗試保存新記錄時,ModelState.IsValid失敗。 我嘗試如下所示添加它們,並分別更新每個表,但這不起作用。
我是MVC,.NET和編程的新手,無法弄清楚我在做什么錯。 任何幫助表示贊賞。
[HttpPost]
public ActionResult Create(Team team)
{
Team t = team;
UpdateModel(t);
if (ModelState.IsValid)
{
teamRepository.AddTeam(t);
teamRepository.Save();
return RedirectToAction("Details", new { id = t.TeamID });
}
return RedirectToAction("Create");
}
這是我正在使用的ViewModel
public class TeamViewModel
{
//Properties
public Team team { get; set; }
public IQueryable<Department> departmentsList { get; set; }
public IQueryable<Team> teamList { get; set; }
public SelectList countriesList { get; set; }
public Country country { get; set; }
//Constructors
public TeamViewModel()
{
}
public TeamViewModel(IQueryable<Team> teams)
{
teamList = teams;
}
public TeamViewModel(TeamViewModel vm)
{
team = vm.team;
departmentsList = vm.departmentsList;
}
public TeamViewModel(TeamViewModel vm, Country c)
{
team = vm.team;
team.CountryID = c.CountryID;
}
public TeamViewModel(TeamViewModel vm, IEnumerable<Country> countries)
{
team = new Team();
countriesList = new SelectList(countries, "CountryID", "ShortName");
}
}
這是在需要時使用的視圖
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<div class="editor-label"><%: Html.LabelFor(m => m.team.ShortName) %></div>
<div class="editor-field"><%: Html.TextBoxFor(m => m.team.ShortName) %><%: Html.ValidationMessageFor(m => m.team.ShortName, "*") %></div>
<div class="editor-label"><%: Html.LabelFor(m => m.team.FullName)%></div>
<div class="editor-field"><%: Html.TextBoxFor(m => m.team.FullName)%><%: Html.ValidationMessageFor(m => m.team.FullName, "*")%></div>
<div class="editor-label"><%: Html.LabelFor(m => m.team.DateEstablished)%></div>
<div class="editor-field"><%: Html.TextBoxFor(m => m.team.DateEstablished)%><%: Html.ValidationMessageFor(m => m.team.DateEstablished, "*")%></div>
<div class="editor-label"><%: Html.LabelFor(m => m.team.City)%></div>
<div class="editor-field"><%: Html.TextBoxFor(m => m.team.City)%><%: Html.ValidationMessageFor(m => m.team.City, "*")%></div>
<div class="editor-label"><%: Html.LabelFor(m => m.team.Country) %></div>
//The misspelled field
//<div class="editor-field"><%: Html.DropDownListFor(model => model.team.Country, Model.countriesList, "<--Select Country-->") %></div>
//The corrected field
<div class="editor-field"><%: Html.DropDownListFor(model => model.team.CountryID, Model.countriesList, "<--Select Country-->") %></div>
</fieldset>
<p>
<input type="submit" value="Save" />
</p>
首先,您正在使用什么存儲庫ORM? 其次,為什么要將字段重新映射到新的Team對象。 Mvc已經為您完成了此操作,我認為您不需要這樣做。 如果您使用的是NHibernate,則可能與設置國家/地區ID(而非國家/地區對象)有關。 如果您有一個國家/地區表,那么您可能會有一個國家/地區表,因此NH將尋找以下內容
team.Country.Id
並不是
team.CountryId
看着你的看法就是這種情況。 如果我是你,我將重新映射Team對象,已經完成了。 暫停操作,檢查國家對象是什么。
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.