I am trying to add new records in my DB to multiple related tables that all have an ID as PK and IDs of other tables as FKs. I've based my approach on the NerdDinner tutorial but I can't find any examples on how to add new records in the DB when more than one table is involved.
ModelState.IsValid fails when trying to save the new records for some reason. I tried adding them as shown below and update each table seperately but that doesn't work.
I am new to MVC, .NET and programming in general and can't figure out what I am doing wrong. Any help is appreciated.
[HttpPost]
public ActionResult Create(Team team)
{
Team t = team;
UpdateModel(t);
if (ModelState.IsValid)
{
teamRepository.AddTeam(t);
teamRepository.Save();
return RedirectToAction开发者_Python百科("Details", new { id = t.TeamID });
}
return RedirectToAction("Create");
}
Here is the ViewModel I am using
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");
}
}
And this is the View in case it is needed
<% 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>
Firstly what Repository ORM are you using? Secondly why are you remapping the fields to a new Team object. Mvc has already done this for you, I don't think you need to do this. If your using something NHibernate then it's probably to do with the setting the country Id and not the country object. If you have a country table the you've probably got a country abject and therefore NH will be look for the the following
team.Country.Id
AND NOT
team.CountryId
Looking at you view this is the case. If I were you I would remap the Team object, already done. Break on the action and check what's the country object.
Hope this helps
精彩评论